0

我已经开始更新 Word for Mac 2016 的加载项,不出所料,它的进展并不顺利。任何人都可以帮助解决这个applescript(或我调用它的方法)可能出现的问题吗?我精通 VBA,但非常熟悉 Applescript 新手。这应该浏览一个或多个文件并返回一个以逗号分隔的文件名字符串。我可以看出 Applescript 本身正在按预期运行(出现通知并给出正确的值)。但是该值似乎并没有出现在 VBA 中,它似乎接收到一个空字符串(尽管鉴于 Word 2016 for Mac 中 VBE 的限制,很难判断)。

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 非常有用的网站上看到的那样,我尝试使用“告诉应用程序“系统事件”返回” ,但这并没有什么不同。我还尝试调用一个非常非常简单的“Hello world”样式 Applescript 来确保我有能力返回任何东西,并且有效(可能没有帮助,但在这里):

on simple(sometext)
  set myText to "Yo"
  display notification myText with title "Hello"
  return myText
end simple

这只是一个大过程的第一步,现在我有点担心。希望有人能指出一些愚蠢的错误,以便我继续前进。

(只是为了一点点上下文,我在 Word 2010 中进行了大部分加载项开发,但它在 Word 2011 中工作。所以,我有很多 MacScript 调用,我正在尝试为 Word 2016 更新。)

4

1 回答 1

1

也许这是一个错误或安全问题,我不知道,但是当您使用VBA 脚本时,您不能使用 ( 或 ) 命令,结果choose ...display dialog始终display alert为空字符串。AppleScriptTaskAppleScriptTask

因此,当您不需要 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
于 2016-05-10T23:57:00.967 回答