1

I have an AppleScript that is used to programmatically create a test script file in one of these Office 2016 app folders:

~/Library/Application Scripts/com.microsoft.Excel
~/Library/Application Scripts/com.microsoft.Word
~/Library/Application Scripts/com.microsoft.Powerpoint

This is the test.scpt file content which is programmatically generated:

on handlerTest(thisPhrase)
    say thisPhrase
end handlerTest

This test.scpt file contains a single handler which speaks the phrase passed to it.

When the script is created in one of these folders, I cannot see the content of the script file in Finder and calling the handler from a Microsoft Office app using the new VBA AppleScriptTask causes the Office app to crash. I think the script is being created as a byte-compiled file because it cannot be viewed in Finder as plain text.

If I then copy the script file generated programmatically by my script creator script to the Documents folder, the plain-text content of the script is viewable in Finder.

Now, if I copy the script file from the Documents folder back to the corresponding com.microsoft folder (without modifying it), I can now see the plain-text content in Finder and calling the handler using the VBA AppleScriptTask function works as expected. I don't understand how the format is apparently changing due to copy/paste actions?

How can I programmatically create the script file in the com.microsoft.xyz folder in plain text format?

Here is my VBA procedure:

Sub TestScript()
    AppleScriptTask "test.scpt", "handlerTest", "hello world"
End Sub

Here is my example script creator script which programmatically creates a test.scpt file in the com.microsoft.Powerpoint scripting folder: (kudos to eliteproxy for the original source script)

property theFolders : {"~/Library/'Application Scripts'/com.microsoft.Powerpoint"}

try
    tell application "Finder" to set targetFolder to (target of the front window) as alias
on error -- no window
    set targetFolder to (choose folder)
end try

# build a parameter string from the folder list
set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space}
set {theFolders, AppleScript's text item delimiters} to {theFolders as text, tempTID}

do shell script "cd " & quoted form of POSIX path of targetFolder & "; mkdir -p " & theFolders

--Write the Script file if it does not exist
if ExistsFile("~/Library/'Application Scripts'/com.microsoft.Powerpoint/test.scpt") is false then
    tell application "Finder"
        --GET THE WORKING DIRECTORY FOR FILE COPY OF SCRIPT
        get folder of (path to me) as Unicode text
        set workingDir to POSIX path of result

        --Write the new script in the current working directory
        set textFile to workingDir & "test.scpt"

        --Delete script if it exists
        set posixPath to POSIX path of textFile as string
        do shell script "rm -rf \"" & posixPath & "\""

        --Create Script Interface file for Microsoft PowerPoint VBA Applications
        set fd to open for access textFile with write permission

        -- Create test handler which speaks the passed phrase parameter
        write "on handlerTest(thisPhrase)" & linefeed to fd as «class utf8» starting at eof
        write "say thisPhrase" & linefeed to fd as «class utf8» starting at eof
        write "end handlerTest" & linefeed to fd as «class utf8» starting at eof

        close access fd

        --Copy the script file into the MACOS-Specific 'safe' folder
        set fromPath to quoted form of POSIX path of (workingDir) & "test.scpt"
        set toPath to quoted form of "~/Library/'Application Scripts'/com.microsoft.Powerpoint"
        do shell script "cp -R " & fromPath & space & "~/Library/'Application Scripts'/com.microsoft.Powerpoint" with administrator privileges
    end tell
end if

--Delete the temp script file from the working directory
set posixPath to POSIX path of textFile as string
do shell script "rm -rf \"" & posixPath & "\""

--Provide confirmation
set theAlertTitle to "TEST"
set theAlertMsg to "The script has been successfully installed."
display alert theAlertTitle message theAlertMsg as informational buttons {"OK"} default button "OK" cancel button "OK"

--For use when checking if a file exists
on ExistsFile(filePath)
    tell application "System Events" to return (exists disk item filePath) and class of disk item filePath = file
end ExistsFile 
braX
  • 11,506
  • 5
  • 20
  • 33
Jamie Garroch - MVP
  • 2,839
  • 2
  • 16
  • 24

1 Answers1

0

I could be wrong in my interpretation of your question, but it appears as if you are looking to create file “Test.scpt” with your handler “handlerTest” as the code, in a folder named “com.microsoft.Excel” (for example). If that is all you are looking to achieve, I believe this solution should work for you...

script theHandler
    on handlerTest(thisPhrase)
        say thisPhrase
    end handlerTest
end script

storeScript()

on storeScript()
    set thisScript to store script theHandler in (path to home folder as text) ¬
        & "Library:Application Scripts:com.microsoft.Excel:Test.scpt" replacing yes
end storeScript
wch1zpink
  • 3,026
  • 1
  • 8
  • 19