2015-05-14 2 views
0

Я использую следующий скрипт в течение нескольких месяцев. Он попросит пользователя выбрать из списка и скопировать текст в MS Word и запустить несколько макросов VB и сохранить файл в виде текстового файла.Applescript Handler

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 

Пожалуйста, дайте мне знать, где я ошибаюсь.

Благодаря Джош

ответ

0

Вы не сказали, какой результат вы получите, когда вы говорите «не работает». Вы получили сообщение об ошибке? Вы ничего не получаете? Получаете ли вы тот же результат, когда вы выбираете «Список PS» по сравнению с другими?

Ошибка, которую я вижу, заключается в том, что вы никогда не переносите Microsoft Word на передний план. Это необходимо, когда вы используете скрипт пользовательского интерфейса и команду нажатия клавиш. Добавьте «активировать» в блоки Word.

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

Кроме того, да, ваш переменный desktopTestFolder теряет область действия. Вы можете сделать переменную глобальным свойством, поставив это в начале вашего скрипта:

property desktopTestFolder: "" 
+0

Я добавил «активировать» в свои блоки Word. Затем он также выдает ошибку «переменная desktopTestFolder не определена». – John

+0

Да, внутри обработчика переменная не сохраняет одно и то же значение. См. Мое редактирование на мой ответ. – jweaks

+0

Спасибо, что работает charmly – John

Смежные вопросы