【问题标题】:Using AppleScript to programmatically create an AppleScript file in plain text format使用 AppleScript 以编程方式创建纯文本格式的 AppleScript 文件
【发布时间】:2018-10-29 06:11:04
【问题描述】:

我有一个 AppleScript,用于以编程方式在这些 Office 2016 应用程序文件夹之一中创建测试脚本文件:

~/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 文件:(感谢 eliteproxy 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 

【问题讨论】:

    标签: vba macos excel applescript ms-office


    【解决方案1】:

    我对您的问题的解释可能是错误的,但看起来您正在寻找使用您的处理程序“handlerTest”作为代码在名为“com.microsoft.Excel”的文件夹中创建文件“Test.scpt” “ (例如)。如果这就是您想要实现的全部目标,我相信这个解决方案应该适合您...

    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
    

    【讨论】:

      猜你喜欢
      • 2016-09-18
      • 2016-01-05
      • 2015-10-23
      • 1970-01-01
      • 2011-02-26
      • 2011-01-18
      • 2010-12-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多