Nel tempo libero (poco) di questi ultimi giorni mi sono concentrato per migliorare lo script “HAL9000”, scipt che uso per lanciare le applicazioni che uso più spesso. Come ho scritto anche qui, c’è un motivo per cui lo script si chiama HAL9000! Inoltre, come ho scritto qui, ho deciso di richiamare l’applicazione tramite comandi vocali, utilizzando un particolare (e utile) camuffamento della finestra di input vocale.
Definite le applicazioni da lanciare e la modalità di input, mi sono concentrato sulla “parametrizzazione” dello script e aumentandone la flessibilità.
Per prima cosa ho pensato che HAL dovesse sapere a chi si sta rivolgendo, prendendo come input il nome breve dell’account attivo su cui si trova a girare. Niente di più facile:

tell application “System Events”
get name of current user
end tell
set nome to result

una volta fatto ciò ho dato ad HAL un set di frasi da utilizzare a seconda della situazione: ci sono risposte affermative (aff), richieste di attenzione (att), risposte negative o di errore (neg), frasi di benvenuto (welk) e di fine script (fin). Naturalmente tutte (o quasi) citazioni dal film 2001: odissea nello spazio.

set aff to {“Affirmative, ” & nome & “, I read you.”, nome & “, I have been putting myself to the fullest possible use”, “O.K., ” & nome & “You’re certainly the boss.”, “Naturally, I will follow all your orders.”}
set itaff to length of aff

set att to {“You’ve been acting a little strange lately, ” & nome & “.”, “Just what do you think you’re doing,” & nome & “?”, “That sounds bad. What can we do?”, “Are you sure you are making the right decision?”}
set itatt to length of att

set neg to {“It can only be attributed to human error.”, “This conversation can serve no purpose anymore. Goodbye.”, “Sorry, security policy does not permit that. Goodbye.”, “I’m sorry ” & nome & “, I’m afraid I can’t do that.”, “I’ve just picked up a fault in the AE-35 unit.”, “t’s going to go 100% failure in 72 hours.”}
set itneg to length of neg

set welk to {“Everything’s running smoothly. And you?”, “Welcome at Home”, “I honestly think you ought to sit down calmly, take a stress pill, and think things over.”, “I’ve still got the greatest enthusiasm and confidence in the mission. And I want to help you”, “I’m completely operational, and all my circuits are functioning perfectly.”, “I can tell from your voice harmonics, ” & nome & “, that you’re badly upset. Why don’t you take a stress pill and get some rest?”, “I could see your lips move.”}
set itwelk to length of welk

set fin to {“All done.”, “This mission is too important for me to allow you to jeopardize it”, “Pod Bay is decompressed. All doors are secure. You are free to open pod bay doors.”, “You now have full manual Macintosh control.”}
set itfin to length of fin

di ogni lista di frasi ho memorizzato il numero di elementi: questo perché successivamente le frasi verranno scelte casualmente utilizzando la funzione random, che però dovrà agire tra 1 (primo elemento) e la lunghezza della lista (cioè l’ultimo elemento).
Lo script prosegue poi con il riconoscimento dell’ora della giornata:

set ora to hours of (current date)
if ora ≤ 13 then
say “Good Morning, ” & nome & “.” using “Zarvox”
else if ora > 13 and ora ≤ 17 then
say “Good Afternoon, ” & nome & “.” using “Zarvox”
else if ora > 17 then
say “Good Evening, ” & nome & “.” using “Zarvox”
end if

quindi ecco il messaggio di benvenuto, scelto a caso da quelli contenuti nella lista “welk”

set i to random number from 1 to itwelk with seed 0
set p to item i of welk
say p using “Zarvox”

bene, ora è venuto il momento di lanciare le applicazioni… prima Mail

tell application “Mail”
activate
check for new mail
delay 1
close windows
end tell

quindi Cashbox

tell application “Cashbox”
activate
delay 0.5
end tell

Ora è venuto il momento di aprire il browser e i link che siamo soliti consultare. In AppleScript c’è una funzione che fa questo, e apre i link direttamente nel browser predefinito. Non dovrete quindi richiamare voi l’apertura del browser, ma farà tutto l’AppleScript:

set indirizzi to {“http://www.bloglines.com/myblogs”, “http://mail.google.com/mail/”, “http://www.macitynet.it/forum/usercp.php”, “http://forum.tevac.com/”, “http://alessio.forumup.it/”}
repeat with indirizzo in indirizzi
open location indirizzo
end repeat

Ora lo script prosegue chiedendomi se voglio aprire o no alcune applicazioni, ad esempio Adium. Prima di tutto richiama la mia attenzione, ma non è indispensabile poiché HAL decide da solo cosa fare se non do’ una risposta entro un certo tempo:

delay 6
set i to random number from 1 to itatt with seed 0
set p to item i of att
say p using “Zarvox”
activate
set diag to display dialog “Do you want to launch Adium?” buttons {“Yes”, “No”} default button 1 with icon 1 with title “HAL 9000 say:” giving up after 5
set risp to button returned of diag
if risp = “Yes” then
set i to random number from 1 to itaff with seed 0
set p to item i of aff
say p using “Zarvox”
tell application “Adium”
activate
end tell
else if risp = “No” then
set i to random number from 1 to itneg with seed 0
set p to item i of neg
say p using “Zarvox”
else
set i to random number from 1 to itaff with seed 0
set p to item i of aff
say p using “Zarvox”
tell application “Adium”
activate
end tell
end if

Ok, ora arriva il difficile. Volevo fare in modo che lo script capisse se l’hard disk esterno su cui ho tutta la musica di iTunes è disponibile oppure no, e in caso affermativo facesse partire iTunes (chiedendomelo prima), restituendo un errore nel caso opposto. Per farlo ho usato qualche comando da terminale

delay 2
— HAL 9000 vuole cantarti una canzone
say “Mr Langley taught me to sing a song. If you’d like to hear it I can sing it for you.” using “Zarvox”
try
do shell script “test -d /Volumes/Media/Music”
activate
set diag to display dialog “Do you want to launch iTunes?” buttons {“Yes”, “No”} default button 1 with icon 1 with title “HAL 9000 say:” giving up after 5
set risp to button returned of diag
if risp = “Yes” then
— questa la capirà solo chi ha visto il film:
say “It’s called Daisy.” using “Zarvox”
tell application “iTunes”
activate
play
close windows
end tell
else if risp = “No” then
set i to random number from 1 to itneg with seed 0
set p to item i of neg
say p using “Zarvox”
else
say “It’s called Daisy.” using “Zarvox”
tell application “iTunes”
activate
play
close windows
end tell
end if
on error
set i to random number from 1 to itneg with seed 0
set p to item i of neg
say p using “Zarvox”
end try

Lo script è quasi finito, manca solo un messaggio che ne comunichi la fine:

delay 1
set i to random number from 1 to itfin with seed 0
set p to item i of fin
say p using “Zarvox”

Salvato lo script come HAL9000 come applicazione, metto l’icona giusta e piazzo tutto in ~/Applications.
Ok, a questo punto ho creato uno script che ho chiamato “Go” che fa più o meno così

on run
tell application “HAL9000”
activate
end tell
end run

l’ho salvato come script e l’ho messo in ~/Libreria/Speech/Speakable Items così posso richiamarlo dal controllo vocale, pronunciano semplicemente “Go”

Se pensate che le istruzioni siano poco chiare e che manchi qualche passaggio… beh, sappiate che è proprio così (anzio, è certamente proprio così, come direbbe un certo Jerry). Infatti non mi sembra giusto darvi la pappa pronta, se non vi date da fare anche voi, poi che gusto c’è??

one more thing…
ho appena scoperto che il blocco

set i to random number from 1 to itwelk with seed 0
set p to item i of welk
say p using “Zarvox”

può essere sostituito più efficacemente da

say some item of welk using “Zarvox”

non si finisce mai di imparare!! 🙂

’nuff said