使用AppleScript以编程方式创建纯文本格式的AppleScript文件

时间:2018-05-18 18:41:52

标签: vba macos excel-vba applescript ms-office

我有一个AppleScript,用于以编程方式在其中一个Office 2016 app文件夹中创建测试脚本文件:

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

这是以编程方式生成的test.scpt文件内容:

on handlerTest(thisPhrase)
    say thisPhrase
end handlerTest

这个test.scpt文件包含一个处理程序,它说出传递给它的短语。

在其中一个文件夹中创建脚本时,我无法在Finder中看到脚本文件的内容,并且使用新的VBA AppleScriptTask从Microsoft Office应用程序调用处理程序会导致Office应用程序崩溃。我认为脚本是作为字节编译的文件创建的,因为它无法在Finder中以纯文本形式查看。

如果我将脚本创建者脚本以编程方式生成的脚本文件复制到Documents文件夹,则可以在Finder中查看脚本的纯文本内容。

现在,如果我将脚本文件从Documents文件夹复制回相应的com.microsoft文件夹(不修改它),我现在可以在Finder中看到纯文本内容并使用VBA AppleScriptTask函数调用处理程序正如所料。我不明白格式是如何因复制/粘贴操作而发生变化的?

如何以纯文本格式在com.microsoft.xyz文件夹中以编程方式创建脚本文件?

这是我的VBA程序:

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

以下是我的示例脚本创建者脚本,它以编程方式在com.microsoft.Powerpoint脚本文件夹中创建test.scpt文件:( eliteproxyoriginal 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 

1 个答案:

答案 0 :(得分:0)

我对你的问题的解释可能是错的,但看起来好像你想用你的处理程序“handlerTest”作为代码创建文件“Test.scpt”,在名为“com.microsoft.Excel”的文件夹中“ (例如)。如果这就是你想要实现的目标,我相信这个解决方案对你有用......

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
相关问题