簡體   English   中英

VBA-在Mac上打開一個文件夾

[英]VBA - Open a folder on Mac

我只想通過VBA打開一個文件夾。 我試過下面的代碼,但它什么也沒做。

對於Windows,我知道以下命令可以正常工作

Call Shell("explorer.exe " & filepath, vbNormalFocus)

這是我正在使用的代碼...

Sub Open_Folder_On_Mac()
Dim folderPath As String
Dim RootFolder As String
Dim scriptstr As String

On Error Resume Next
RootFolder = MacScript("return (path to desktop folder) as String")

scriptstr = "do shell script ""open " & RootFolder & """"

MacScript (scriptstr)
End Sub 

您能幫我得到在Mac上簡單地打開文件夾的代碼嗎? 謝謝!

AppleScript通過告訴特定應用程序執行應用程序開發人員提供的腳本字典(如果有)中公開的一個或多個命令來完成這些事情。 對於Finder (始終運行),它將是:

tell application "Finder" to open theFolder

其中, folderaliasfilePOSIX file對象,例如來自:

set theFolder to (choose folder)

請注意, open shell實用程序通常用於打開文件和應用程序。


未經測試的示例:

較舊(不建議使用)的樣式:

•請注意,此命令在沙盒應用程序中可能不起作用。

Dim ScriptString as String
Dim FolderPath as String

ScriptString = "tell application " & Chr(34) & "Finder" &  Chr(34) & " to open folder (path to desktop)"
-- or --
FolderPath = "Macintosh HD:Users:you:Path:To:Some:Folder:"
ScriptString =  "tell application " & Chr(34) & "Finder" &  Chr(34) & " to open folder " & FolderPath

MacScript(ScriptString)

較新的(2016+)樣式:

•使用腳本編輯器創建一個腳本,該腳本包含要調用的處理程序(子例程),例如:

on openFolder(folderPath)
    tell application "Finder" to open folder folderPath
end openFolder

•將腳本放在用戶的~/Library/Application Scripts/[bundle id]文件夾中,其中[bundle id]是使用該腳本的應用程序的捆綁標識符。

•新命令的格式為AppleScriptTask("script file name.scpt", "handler name", "handler argument") ,例如:

Dim FolderPath as String

FolderPath = "Macintosh HD:Users:you:Path:To:Some:Folder:"
AppleScriptTask("MyScript.scpt", "openFolder", FolderPath)

請參閱使用VB運行AppleScript

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM