Search code examples
vbamacosms-wordapplescriptoffice-2016

Returning a value with Applescripttask (Word for Mac 2016)


I've started updating an add-in for Word for Mac 2016 and, unsurprisingly, it's not going so well. Can anyone assist in what might be wrong with this applescript (or my method of invoking it)? I'm well-versed in VBA, but very much an Applescript newbie. This should browse for a file or files and return a comma-delimited string of file names. I can tell that the Applescript itself is running as expected (the notification appears and gives a correct value). But the value does not seem to make it to VBA, which seems to receive an empty string (although it's hard to tell given the limitations of the VBE in Word 2016 for Mac).

VBA (simplified for brevity):

#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 (this is within a file named as noted above, placed in the correct location):

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

I've made little changes to the notification to ensure that it isn't some sort of caching issue with the Applescript. The file browser basically works (it sometimes gets stuck and won't allow a selection to be made, but that seems to be separate issue). I tried using 'tell application "System Events" to return" as I saw on Ron DeBruin's very helpful site, but that didn't make a difference. I also tried calling a very, very simple "Hello world" style Applescript to make sure I was capable of returning anything at all, and that worked (probably not helpful, but here it is):

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

This is only the first step in a big process, and now I'm a bit worried. Hopefully someone can point out some dumb error so that I can move on.

(Just for a little bit of context, I do most of my add-in development in Word 2010, but it works in Word 2011. So, I have a lot of MacScript calls that I'm trying to update for Word 2016.)


Solution

  • Maybe it's a bug or a security issue, I don't know, but you can't use the (choose ..., display dialog or display alert) commands when you use the AppleScriptTask from a VBA script, the result of the AppleScriptTask will always be an empty string.

    So, you can use these commands when you doesn't need a result in the VBA variable.


    Example (the result of the AppleScriptTask command is an empty string when it run this 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
    

    You can use the MacScript() command to choose some file (It still works on 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