Applescript Handler

时间:2015-05-14 15:47:28

标签: 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 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

请告诉我,我哪里错了?

由于 约什

1 个答案:

答案 0 :(得分:0)

当你说“它不起作用”时,你没有说出你得到的结果。你收到错误了吗?你什么都没得到?当你选择“PS列表”而不是其他人时,你会得到相同的结果吗?

我看到的错误是你永远不会把Microsoft Word带到前面。当您使用UI脚本和击键命令时,这是必需的。将“激活”添加到Word告诉块。

tell application "Microsoft Word"
    activate
    set theContent to content of text object of selection
    …

此外,是的,您的变量desktopTestFolder会丢失范围。您可以将变量放在脚本的前面来使变量成为全局属性:

property desktopTestFolder: ""