====== AppleScript ======
===== Mein erstes AppleScript =====
Beispiel: Backup-Starten (macup.sh) und Runterfahren:
* Skripteditor starten
* Script:
set ACTION to 0
display dialog the ("Backup und Ausschalten.") buttons {"Abbruch", "nur Backup", "Ausschalten"} default button 3
if the button returned of the result is "nur Backup" then
set ACTION to 1
else
if the button returned of the result is "Ausschalten" then
set ACTION to 2
end if
end if
if ACTION > 0 then
set OUT to do shell script ("~/macup/macup.sh 2>&1")
(* bei Fehler Abbruch durch AppleScript, in diesem Fall ok *)
if ACTION = 1 then
display dialog "Backup OK
" & OUT buttons {"Fertig"} default button 1
end if
end if
if ACTION > 1 then
display dialog ("Rechner herunterfahren?")
(* tell application "Finder" to shut down *)
end if
* Speichern als "Programm" (dann kann mans im Finder durch Doppelklicken starten)
* "Programm" auf Dock ziehen und Starten
===== Starte Terminal und ssh =====
tell application "Terminal"
do script with command "ssh user@server.de"
set name_of_window to (get name of front window)
set id_of_window to (get id of front window)
set active_tab_of_window to (get selected tab of window id id_of_window) as record
set active_tab_of_window_record to active_tab_of_window as list
set active_tab_of_window_record_id to (get item 3 of active_tab_of_window_record) as integer
set custom title of tab active_tab_of_window_record_id of window id id_of_window to "user@server.de"
activate
end tell
* "fast alles am Ende" ist nur um den titel zu setzen. Geht das auch einfacher ?
* "activate" bringt Applikation nach vorne (alle Fenster)
2. Version: ermittel den eigenen Dateinamen, um damit den Zielrechner zu bestimmen (dann reich ein einfaches Kopieren der Datei für einen neuen Server).
tell application "iTerm"
# eigenen Dateinamen ermitteln (ohne Pfad und ohne .app) -> myName
set myPath to path to me as text
if myPath ends with ":" then
set n to -2
else
set n to -1
end if
set AppleScript's text item delimiters to ":"
set myName to text item n of myPath
if (myName contains ".") then
set AppleScript's text item delimiters to "."
set myName to text 1 thru text item -2 of myName
end if
set AppleScript's text item delimiters to ""
set Hostname to myName
set Terminalfenster_1 to (make new terminal)
tell Terminalfenster_1
#set number of columns to 80
#set number of rows to 25
set Tab_1 to (make new session at the end of sessions)
tell Tab_1
activate
set name to Hostname
#set background color to "black"
#set foreground color to "green"
#set transparency to "0.1"
exec command "/bin/bash"
write text "ssh " & Hostname
end tell -- Tab_1
end tell -- Terminalfenster_1
end tell
===== Starte Chicken of the Vnc Sitzung =====
do shell script ("\"/Applications/Chicken of the VNC.app/Contents/MacOS/Chicken of the VNC\" --Display 25124 chicago.sscvp.net &> /dev/null &")
Besonderheiten:
* Script läuft im Hintergrund (''&> /dev/null &'')
* ''\"'' um Scriptname, wegen der Leerzeichen
* Parameter für Chicken (starte Programm direkt mit ''--help'' zeigt Syntax)
===== Mail.app: Account (de-)aktivieren =====
tell application "Mail"
set ist_aktiv to (get enabled of account "NUREG")
set enabled of account "NUREG" to not ist_aktiv
end tell
===== iTerm mit mehreren Tabs und mit Kommandos =====
set box_user to "kfr"
set my_boxes to {„host1.de“, „host2.de“}
set ssh_opt to "-L 4040:localhost:4040 -p 23765 "
set my_cmd to {"sudo bash", "apt-get update", "apt-get upgrade -V", "checkrestart"}
tell application "iTerm"
activate
set t to (make new terminal)
tell t
(* Loop over the boxes, create a new tab and connect. *)
repeat with box in my_boxes
activate current session
launch session "Default Session"
tell the last session
set conn to "ssh " & ssh_opt & box_user & "@" & box
set name to box_user & "@" & box
write text conn
repeat with cmd in my_cmd
write text cmd
end repeat
end tell
end repeat
end tell
end tell
===== Links =====
* http://developer.apple.com/technotes/tn2002/tn2065.html
* [[applescriptcopy]]