Applescript处理程序

Applescript处理程序,applescript,Applescript,我使用下面的脚本几个月了。它要求用户从列表中选择并复制粘贴MS Word中的文本,运行一些VB宏并将文件保存为文本文件 tell application "Finder" if not (exists folder "Test" of desktop) then make new folder at desktop with properties {name:"Test"} end tell set desktopTestFolder to (path to desktop folder

我使用下面的脚本几个月了。它要求用户从列表中选择并复制粘贴MS Word中的文本,运行一些VB宏并将文件保存为文本文件

tell application "Finder"
    if not (exists folder "Test" of desktop) then make new folder at desktop with properties {name:"Test"}
end tell
set desktopTestFolder to (path to desktop folder as text) & "Test:"
set mychoice to (choose from list {"PS List", "AA Table", "PS Legend", "PO Chart", "MD"} with prompt "Please select which sound you like best" default items "None" OK button name {"Play"} cancel button name {"Cancel"})
if mychoice is false then error number -128 -- user canceled

    tell application "Microsoft Word"
        set theContent to content of text object of selection
        copy object text object of selection
        set newDoc to make new document
        delay 2
        tell application "System Events"
            tell process "Microsoft Word"
                keystroke "v" using command down
            end tell
        end tell
        run VB macro macro name "Normal.NewMacros.Clean"
        run VB macro macro name "Normal.Module9.bold"

        save as newDoc file format format Unicode text file name (desktopTestFolder & mychoice & ".txt")
        close document 1 saving no
    end tell
但当我试着把它放进处理器时,它就不起作用了。我尝试的是:

tell application "Finder"
    if not (exists folder "Test" of desktop) then make new folder at desktop with properties {name:"Test"}
end tell
set desktopTestFolder to (path to desktop folder as text) & "Test:"
set mychoice to (choose from list {"PS List", "AA Table", "PS Legend", "PO Chart", "MD"} with prompt "Please select which sound you like best" default items "None" OK button name {"Play"} cancel button name {"Cancel"})
if mychoice is false then error number -128 -- user canceled

set mychoice to mychoice as text
if mychoice is equal to "PS List" then
    handler1()
else
    handler2()
end if
on handler1()
    tell application "Microsoft Word"
        set theContent to content of text object of selection
        copy object text object of selection
        set newDoc to make new document
        delay 2
        tell application "System Events"
            tell process "Microsoft Word"
                keystroke "v" using command down
            end tell
        end tell
        run VB macro macro name "Normal.NewMacros.EDCleanup1"
        run VB macro macro name "Normal.Module9.bold"

        save as newDoc file format format Unicode text file name (desktopTestFolder & mychoice & ".txt")
        close document 1 saving no
    end tell
end handler1

on handler2()
    tell application "Microsoft Word"
        run VB macro macro name "Normal.NewMacros.EDCleanup1"
        run VB macro macro name "Normal.Module9.bold"

        save as newDoc file format format Unicode text file name (desktopTestFolder & mychoice & ".txt")
        close document 1 saving no
    end tell
end handler2
请让我知道,我错在哪里

谢谢
Josh

当你说它不起作用时,你没有说你得到了什么结果。你有错误吗?你什么也得不到吗?当你选择PS列表和其他列表时,你会得到相同的结果吗

我看到的错误是,您从未将Microsoft Word带到前台。当您使用UI脚本和击键命令时,这是必需的。将“激活”添加到Word-tell块

tell application "Microsoft Word"
    activate
    set theContent to content of text object of selection
    …
另外,是的,您的变量desktopTestFolder失去作用域。您可以将该变量设置为全局属性,方法是将其放置在脚本的前面:

property desktopTestFolder: ""

我将“激活”添加到我的Word-tell块中。然后它还会抛出一个错误,即变量desktopTestFolder未定义。是的,在处理程序中,该变量不保留相同的值。请参阅我对我的答案的编辑。