Use launchctl para disparar um script AppleScript periodicamente

1

Eu escrevi um AppleScript que me permite fazer backup de um determinado arquivo. O script roda bem dentro do AppleScript Editor: ele faz o que deve fazer perfeitamente. Até aí tudo bem.

Agora, gostaria de executar este script em intervalos de tempo. Então eu uso launchctl & .plist para fazer isso acontecer. É aí que o problema começa.

  • o script é carregado no intervalo definido por launchctl
  • o editor AppleScript (quando aberto) traz sua janela (com aquele script) para o primeiro plano, mas nenhum código é executado
  • quando o AppleScript Editor está não em execução, nada parece estar acontecendo

Alguma idéia de por que isso não está funcionando?

-

Após a edição (de acordo com as sugestões de Daniel Beck), meu problema agora se parece com:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>KeepAlive</key>
<false/>
<key>Label</key>
<string>com.opera.autosave</string>
<key>ProgramArguments</key>
<array>
<string>osascript</string>
<string>/Users/user_name/Library/Scripts/opera_autosave_bak.scpt</string>
</array>
<key>StartInterval</key>
<integer>30</integer>
</dict>
</plist>

e o AppleScript que estou tentando executar:

on appIsRunning(appName)
    tell application "System Events" to (name of processes) contains appName
end appIsRunning

--only run this script when Opera is running
if appIsRunning("Opera") then
    set base_path to "user_name:Library:Preferences:Opera Preferences:sessions:"
    set autosave_file to "test.txt"
    set autosave_file_old to "test_old.txt"
    set autosave_file_older to "test_older.txt"
    set autosave_file_oldest to "test_oldest.txt"
    set autosave_path to base_path & autosave_file
    set autosave_path_old to base_path & autosave_file_old
    set autosave_path_older to base_path & autosave_file_older
    set autosave_path_oldest to base_path & autosave_file_oldest
    set copied_file to "test copy.txt"
    set copied_path to base_path & copied_file

    tell application "Finder"
        duplicate file autosave_path
        delete file autosave_path_oldest
        set name of file autosave_path_older to autosave_file_oldest
        set name of file autosave_path_old to autosave_file_older
        set name of file copied_path to autosave_file_old
    end tell

end if
    
por Daktari 13.04.2011 / 09:51

2 respostas

1

Salve o script como aplicativo no editor AppleScript ( Arquivo »Salvar como… ) ou altere a chamada em seu launchd plist para abrir osascript (o modo Terminal de executar o AppleScript) com o arquivo de script como argumento.

    
por 13.04.2011 / 09:55
0

Parece ser afetado por isso:

So if you run a script “by hand”—whether from AppleScript Editor, from within Automator, or as a standalone app or droplet—it should be able to do whatever it’s scripted to do, just as it can today. Put another way, you should be able to continue to run scripts by hand just as you always have.

Internal application scripts: Some apps use “internal” AppleScripts to handle certain actions of their own. (For example, BBEdit ( ) uses such scripts when it installs its command-line tools.) Such scripts are built into the app; you never see them in menus or elsewhere. Such self-referential scripts should continue to work as they always have.

If, however, a sandboxed app wants to use AppleScript to interact with another app or with other parts of your system—a menubar app that uses AppleScripts to control iTunes, say—then the new restrictions will come into play. A sandboxed app can’t use AppleScript to communicate with another app on your Mac, unless the developer specifically requests (and receives) an entitlement to do just that.

link

    
por 04.02.2015 / 15:46