2013-03-24 13 views
16

Sto cercando di eseguire uno script AppleScript dal terminale, ma io non riesco a farlo stampa nulla chiamandoStampa per Stdout con AppleScript

osascript myFile.scpt "/path/to/a/file" 

Sto cercando:

on run fileName 

set unique_songs to paragraphs of (read POSIX file fileName) 

repeat with nextLine in unique_songs 
    if length of nextLine is greater than 0 then 
     set AppleScript's text item delimiters to tab 
     set song to text item 2 of nextLine 
     set artist to text item 3 of nextLine 
     set album to text item 4 of nextLine 

     set output to ("Song: " & song & " - " & artist & " - " & album) 
     copy output to stdout 
    end if 
end repeat 
end run 

Il file delimitato scheda è formattato o meno così:

1282622675 Beneath the Balcony Iron & Wine The Sea & the Rhythm  
1282622410 There Goes the Fear Doves (500) Days of Summer   
1282622204 Go to Sleep. (Little Man Being Erased.) Radiohead Hail to the Thief 

schede non stanno mostrando davvero bene su questo :(

+0

possibile duplicato di [Stampa su stdout da osascript/Applescript] (http://stackoverflow.com/questions/8766868/print-to-stdout-from-osascript-applescript) –

risposta

7

Non è molto chiaro come si sta tentando di eseguirlo in Termanil. Ma supporrò che tu abbia salvato un file di testo di mele con #!/Usr/bin/osascript, chmod 'ed il file per poterlo eseguire.

Quindi chiamato il file nel terminale. semplicemente usando il percorso del file.

Aggiornato: utilizzare eco

  #!/usr/bin/osascript 
    #Here be the rest of your code ... 

     set output to ("Song: " & song & " - " & artist & " - " & album) 

    do shell script "echo " & quoted form of output 
end tell 

aggiornamento 2, in reponse ai commenti.

Se ho un file di testo scheda delimitata con il contenuto come:

track Skin Deep Beady Belle Closer 

Le schede sono incastonato come: pista * TABSkin DeepTABBeady BelleTAB * Closer

e il file di script come:

on run fileName 


    set unique_songs to paragraphs of (read POSIX file fileName) 

    repeat with nextLine in unique_songs 
     if length of nextLine is greater than 0 then 
      set AppleScript's text item delimiters to tab 
      set song to text item 2 of nextLine 
      set artist to text item 3 of nextLine 
      set album to text item 4 of nextLine 

      set output to ("Song: " & song & " - " & artist & " - " & album) 
      do shell script "echo " & quoted form of output 
     end if 
    end repeat 



end run 

il terminale corsa:

/usr/bin/osascript ~/Documents/testOsa2.scpt ~/Documents/testTab.txt 

torno: Song: Skin Deep - Beady Belle - Closer

+0

Hm. Neanche sembra funzionare. Aggiornato con come lo sto eseguendo. Grazie per avermelo fatto notare :) – Kristin

+0

Vedo. Che ne dici di pubblicare più codice. Quale può aiutare a farsi un'idea migliore di ciò che stai facendo. Perché entrambi i modi di correre dovrebbero produrre qualcosa. Cosa stai tornando nel terminale. – markhunte

+0

Sì, quindi non ricevo solo errori se lo script si è arrestato in modo anomalo nel terminale, tuttavia quando lo eseguo in AppleScript Editor, vengono fuori tutti i dati del registro. Posterò altro codice – Kristin

12

Quando eseguendo AppleScript con #!/usr/bin/osascript, puoi semplicemente restituire l'output di testo desiderato con un'istruzione return alla fine del tuo script.

+2

Questo è un approccio molto più semplice rispetto alla generazione di un processo shell separato solo per stampare l'output di un altro. Nell'esempio precedente, sembrerebbe come 'restituire il form quotato dell'output' proprio prima della riga' end run'. – Beejor

+0

questo è molto meglio. Grazie – fabrizioM

+5

Ma sicuramente sarebbe bello poter emettere solo blocchi di testo mentre vengono incontrati, piuttosto che doverli accumulare tutti in una grande stringa, e poi dover restituire quella grande stringa alla fine. –

Problemi correlati