使用 Applescripttask 返回一个值(Word for Mac 2016)
Returning a value with Applescripttask (Word for Mac 2016)
我已经开始更新 Mac 2016 年的 Word 加载项,不出所料,进展并不顺利。任何人都可以协助解决此 applescript(或我调用它的方法)可能出现的问题吗?我精通 VBA,但我是 Applescript 的新手。这应该浏览一个或多个文件和 return 逗号分隔的文件名字符串。我可以看出 Applescript 本身是预期的 运行(通知出现并给出正确的值)。但是该值似乎没有达到 VBA,它似乎收到一个空字符串(尽管很难判断,因为 Word 2016 中 VBE 对 Mac 的限制)。
VBA(为简洁起见进行了简化):
#If MAC_OFFICE_VERSION >= 15 Then
Dim args As String, MyFiles As String
'These variables have been set elsewhere and I can confirm with Debug.Print that they are as expected.
args = MyBrowseTypes & ";" & browseMulti & ";" & browsePath
MyFiles = Applescripttask("scrHelperAS.scpt", "browseFiles", args)
' This prints true, for what that's worth
If MyFiles = "" Then
Debug.Print "True"
Else: Debug.Print "False"
End If
#End If
Applescript(这是在如上所述命名的文件中,放置在正确的位置):
on browseFiles(argString)
--open file browser and return selection
set {sFileType, bMultiples, sDefPath} to SplitString(argString, ";")
set sFileTypes to SplitString(sFileType, ",")
if bMultiples is "true" then
set thePrompt to "Please select a file or files"
else
set thePrompt to "Please select a file"
end if
set AppleScript's text item delimiters to ","
set theFiles to (choose file of type sFileTypes with prompt thePrompt multiple selections allowed bMultiples default location alias POSIX file (sDefPath)) as string
' I don't know if this is/should be necessary, added to try to fully coerce the return value to a string. Didn't work without it, still doesn't work with it.
set theFilesStr to joinList(theFiles, ",")
set AppleScript's text item delimiters to ""
display notification theFilesStr with title "Files"
return theFilesStr
end browseFiles
我对通知做了一些改动,以确保它不是 Applescript 的某种缓存问题。文件浏览器基本上可以工作(它有时会卡住并且不允许进行选择,但这似乎是一个单独的问题)。正如我在 Ron DeBruin's very helpful site 上看到的那样,我尝试使用“告诉应用程序 "System Events" 到 return”,但这并没有什么不同。我还尝试调用一个非常非常简单的 "Hello world" 样式 Applescript 以确保我能够 returning 任何东西,并且有效(可能没有帮助,但在这里它是):
on simple(sometext)
set myText to "Yo"
display notification myText with title "Hello"
return myText
end simple
这只是一个大过程中的第一步,现在我有点担心。希望有人能指出一些愚蠢的错误,以便我继续前进。
(只是为了一点上下文,我在 Word 2010 中进行大部分加载项开发,但它在 Word 2011 中工作。所以,我有很多 Mac 脚本调用,我我正在尝试更新 Word 2016。)
可能是bug或者安全问题,我不知道,但是你在使用的时候不能使用(choose ...
, display dialog
or display alert
) 命令来自 VBA 脚本的 AppleScriptTask
,AppleScriptTask
的结果将始终为空字符串。
因此,当您不需要 VBA 变量中的结果时,您可以使用这些命令。
示例(AppleScriptTask命令的结果是一个空字符串,当它运行这个AppleScript时:
on simple(sometext)
set myText to sometext as string
display notification myText with title "Hello"
return text returned of (display dialog "Type some word" default answer myText)
end simple
您可以使用 MacScript() 命令来选择一些文件(它仍然适用于 Microsoft Office 2016):
browsePath = "/Users/myUserName/Documents/someFolder/"
MyBrowseTypes = """xls"", ""doc"""
browseMulti = True
If browseMulti Then
myPrompt = "Please select a file or files"
Else
myPrompt = "Please select a file"
End If
myScript = "set theFiles to (choose file of type {" & MyBrowseTypes & "} " & _
"with prompt """ & myPrompt & """ default location (""" & _
browsePath & """ as posix file as alias) multiple selections allowed " & browseMulti & ")" & vbNewLine & _
"set {TID, text item delimiters} to {text item delimiters, "",""}" & vbNewLine & _
"set theFiles to theFiles as text" & vbNewLine & _
"set text item delimiters to TID" & vbNewLine & _
"return theFiles"
MyFiles = MacScript(myScript)
Debug.Print MyFiles
我已经开始更新 Mac 2016 年的 Word 加载项,不出所料,进展并不顺利。任何人都可以协助解决此 applescript(或我调用它的方法)可能出现的问题吗?我精通 VBA,但我是 Applescript 的新手。这应该浏览一个或多个文件和 return 逗号分隔的文件名字符串。我可以看出 Applescript 本身是预期的 运行(通知出现并给出正确的值)。但是该值似乎没有达到 VBA,它似乎收到一个空字符串(尽管很难判断,因为 Word 2016 中 VBE 对 Mac 的限制)。
VBA(为简洁起见进行了简化):
#If MAC_OFFICE_VERSION >= 15 Then
Dim args As String, MyFiles As String
'These variables have been set elsewhere and I can confirm with Debug.Print that they are as expected.
args = MyBrowseTypes & ";" & browseMulti & ";" & browsePath
MyFiles = Applescripttask("scrHelperAS.scpt", "browseFiles", args)
' This prints true, for what that's worth
If MyFiles = "" Then
Debug.Print "True"
Else: Debug.Print "False"
End If
#End If
Applescript(这是在如上所述命名的文件中,放置在正确的位置):
on browseFiles(argString)
--open file browser and return selection
set {sFileType, bMultiples, sDefPath} to SplitString(argString, ";")
set sFileTypes to SplitString(sFileType, ",")
if bMultiples is "true" then
set thePrompt to "Please select a file or files"
else
set thePrompt to "Please select a file"
end if
set AppleScript's text item delimiters to ","
set theFiles to (choose file of type sFileTypes with prompt thePrompt multiple selections allowed bMultiples default location alias POSIX file (sDefPath)) as string
' I don't know if this is/should be necessary, added to try to fully coerce the return value to a string. Didn't work without it, still doesn't work with it.
set theFilesStr to joinList(theFiles, ",")
set AppleScript's text item delimiters to ""
display notification theFilesStr with title "Files"
return theFilesStr
end browseFiles
我对通知做了一些改动,以确保它不是 Applescript 的某种缓存问题。文件浏览器基本上可以工作(它有时会卡住并且不允许进行选择,但这似乎是一个单独的问题)。正如我在 Ron DeBruin's very helpful site 上看到的那样,我尝试使用“告诉应用程序 "System Events" 到 return”,但这并没有什么不同。我还尝试调用一个非常非常简单的 "Hello world" 样式 Applescript 以确保我能够 returning 任何东西,并且有效(可能没有帮助,但在这里它是):
on simple(sometext)
set myText to "Yo"
display notification myText with title "Hello"
return myText
end simple
这只是一个大过程中的第一步,现在我有点担心。希望有人能指出一些愚蠢的错误,以便我继续前进。
(只是为了一点上下文,我在 Word 2010 中进行大部分加载项开发,但它在 Word 2011 中工作。所以,我有很多 Mac 脚本调用,我我正在尝试更新 Word 2016。)
可能是bug或者安全问题,我不知道,但是你在使用的时候不能使用(choose ...
, display dialog
or display alert
) 命令来自 VBA 脚本的 AppleScriptTask
,AppleScriptTask
的结果将始终为空字符串。
因此,当您不需要 VBA 变量中的结果时,您可以使用这些命令。
示例(AppleScriptTask命令的结果是一个空字符串,当它运行这个AppleScript时:
on simple(sometext)
set myText to sometext as string
display notification myText with title "Hello"
return text returned of (display dialog "Type some word" default answer myText)
end simple
您可以使用 MacScript() 命令来选择一些文件(它仍然适用于 Microsoft Office 2016):
browsePath = "/Users/myUserName/Documents/someFolder/"
MyBrowseTypes = """xls"", ""doc"""
browseMulti = True
If browseMulti Then
myPrompt = "Please select a file or files"
Else
myPrompt = "Please select a file"
End If
myScript = "set theFiles to (choose file of type {" & MyBrowseTypes & "} " & _
"with prompt """ & myPrompt & """ default location (""" & _
browsePath & """ as posix file as alias) multiple selections allowed " & browseMulti & ")" & vbNewLine & _
"set {TID, text item delimiters} to {text item delimiters, "",""}" & vbNewLine & _
"set theFiles to theFiles as text" & vbNewLine & _
"set text item delimiters to TID" & vbNewLine & _
"return theFiles"
MyFiles = MacScript(myScript)
Debug.Print MyFiles