Hi all,
I have a script that uses Terminal in some sections, and because I need to wait for Terminal to finish its processes before I carry on with any more applescript code I’ve been using this:
tell application "Terminal"
do script "lsof; osascript -l AppleScript -e " & quoted form of ("tell application \"Terminal\" to close (first window whose frontmost is true)") in window 1
-- wait until the window is gone:
repeat while (exists window 1)
end repeat
end tell
This works fine, waiting for all commands to be executed in the Terminal window and then closes it, using version 1.3.3 of Terminal. I’m now trying to run the same code on a machine that’s running version 1.5 and when the window tries to close it presents a dialog window saying “Closing this window will terminate the following processes inside it: login, bash, osasript” with a Cancel and Terminate buttons.
Any ideas why this is happening? is it the version? how can I prevent it?
Thanks,
Nik
Sorry, I found out why. I needed to change the Processes settings to Prompt before closing window: never, under the Window Settings in Terminal.
Thanks for viewing
I found that above code some time ago and incorporated it into my script, it’s been working fine but now that I’m trying to use the code on another machine I’m getting odd results like the repeat loop not existing when there is no Terminal window but on my machine it’s working fine. If there’s a better way to check that terminal script has completed then please let me know he’s my complete code.
global files_to_process
on idle
Terminal_Check()
try
tell application "Finder"
set sourcefolder to folder "Export_Objects:Downsample:Success" as alias
set inputfolder to folder "Export_Objects:Downsample:Input" as alias
set CombinedPDFfolder to folder "Export_Objects:Combined PDF" as alias
end tell
on error the_error
error_Handler(the_error)
end try
tell application "Finder"
set Success_PDF_List to name of every file of sourcefolder
if Success_PDF_List ≠{} then
set search_string to characters 1 thru 14 of item 1 of Success_PDF_List as string
else
error number -128
end if
set Input_PDF_List to name of every file of inputfolder
if Input_PDF_List = {} then
my Combine_PDFs(sourcefolder, search_string, CombinedPDFfolder)
else if (Input_PDF_List as string) contains search_string then
repeat until Input_PDF_List as string does not contain search_string
delay 30
set Input_PDF_List to name of every file of inputfolder
end repeat
my Combine_PDFs(sourcefolder, search_string, CombinedPDFfolder)
else if (Input_PDF_List as string) does not contain search_string then
my Combine_PDFs(sourcefolder, search_string, CombinedPDFfolder)
end if
end tell
end idle
on Combine_PDFs(sourcefolder, search_string, CombinedPDFfolder)
set startpath to "/Volumes/Export_Objects/temp/"
set filepath1 to "/Volumes/Export_Objects/Downsample/Success"
set destpath to startpath & search_string & "-" & "combined.pdf"
tell application "Finder"
set files_to_process to name of every file of the sourcefolder whose name contains search_string
set RawScript to "joinPDF " & "'" & destpath & "'" & space
repeat with thefile in files_to_process
set fullpath to "'" & filepath1 & "/" & thefile & "'" & space
set RawScript to RawScript & "'" & filepath1 & "/" & thefile & "'" & space
end repeat
end tell
tell application "Terminal"
do script RawScript in window 1
end tell
tell application "Terminal"
do script "lsof; osascript -l AppleScript -e " & quoted form of ("tell application \"Terminal\" to close (first window whose frontmost is true)") in window 1
-- wait until the window is gone:
repeat while (exists window 1)
end repeat
end tell
set deletescript to "rm "
repeat with thenewfile in files_to_process
set mynewfullpath to "'" & filepath1 & "/" & thenewfile & "'" & space
set deletescript to deletescript & filepath1 & "/" & thenewfile & space
end repeat
tell application "Terminal"
do script ""
do script deletescript in window 1
end tell
tell application "Terminal"
do script "lsof; osascript -l AppleScript -e " & quoted form of ("tell application \"Terminal\" to close (first window whose frontmost is true)") in window 1
-- wait until the window is gone:
repeat while (exists window 1)
end repeat
end tell
try
tell application "Finder"
set combinedPDF_alias to "Export_Objects:temp:" & search_string & "-" & "combined.pdf" as alias
move combinedPDF_alias to CombinedPDFfolder with replacing
end tell
on error the_error
error_Handler(the_error)
end try
end Combine_PDFs
on error_Handler(the_error)
set current_user to system attribute "USER"
tell application "Mail"
set rbiMessage to make new outgoing message with properties {subject:current_user & " encounterd problems", content:the_error}
tell rbiMessage
set visible to true
make new to recipient at end of to recipients with properties {name:"CombinePDF", address:" nikj@jjays.com"}
send
end tell
end tell
error number -128
end error_Handler
on Terminal_Check()
tell application "Finder" to name of processes
if the result does not contain "Terminal" then
tell application "Terminal" to launch
else
tell application "Terminal"
set window_num to number of windows
if window_num > 1 then
close every window
do script ""
else if window_num < 1 then
do script ""
end if
end tell
end if
end Terminal_Check
I ask again: Why don’t you use do shell script instead of poking around the terminal?
AppleScript waits until the shell line has been completed and then executes the next line.
Test this:
set a to do shell script "lsof"
say "done"
display dialog (paragraphs 1 thru 10 of a) as Unicode text
Hi Stefan,
because I’m using joinPDF (a third party product) I was working from examples that I found for the product and also I wasn’t aware that applescript would wait for a shell script to finish before executing any further Applescript commands hence why I was using the original code I posted to check when the joinPDF process had finished. I’ll have a look at rewriting the code without using Terminal if you think this process is possible?
Thanks as always for your direction,
Nik
Thanks Stefan, you’re right again as usual!!! I’ve taken out some of the error handlers and changed the paths for now but this script works well and without and use of Terminal
--try
tell application "Finder"
set sourcefolder to folder "Export_Objects:Downsample:Success" as alias
set inputfolder to folder "Export_Objects:Downsample:Input" as alias
set CombinedPDFfolder to folder "Export_Objects:Combined PDF" as alias
end tell
tell application "Finder"
set Success_PDF_List to name of every file of sourcefolder
if Success_PDF_List ≠{} then
set search_string to characters 1 thru 14 of item 1 of Success_PDF_List as string
else
error number -128
end if
set Input_PDF_List to name of every file of inputfolder
if Input_PDF_List = {} then
my Combine_PDFs(sourcefolder, search_string, CombinedPDFfolder)
else if (Input_PDF_List as string) contains search_string then
repeat until Input_PDF_List as string does not contain search_string
delay 30
set Input_PDF_List to name of every file of inputfolder
end repeat
my Combine_PDFs(sourcefolder, search_string, CombinedPDFfolder)
else if (Input_PDF_List as string) does not contain search_string then
my Combine_PDFs(sourcefolder, search_string, CombinedPDFfolder)
end if
end tell
on Combine_PDFs(sourcefolder, search_string, CombinedPDFfolder)
set startpath to (path to desktop as string) & "Export_Objects:temp" as alias
set startpathPOSIX to POSIX path of startpath
set filepath1 to (path to desktop as string) & "Export_Objects:Downsample:Success"
set filepath1POSIX to POSIX path of filepath1
set destpath to startpathPOSIX & search_string & "-" & "combined.pdf"
tell application "Finder"
set files_to_process to name of every file of the sourcefolder whose name contains search_string
set RawScript to "joinPDF " & "'" & destpath & "'" & space
repeat with thefile in files_to_process
set fullpath to "'" & filepath1POSIX & "/" & thefile & "'" & space
set RawScript to RawScript & "'" & filepath1POSIX & "/" & thefile & "'" & space
end repeat
end tell
do shell script RawScript
set deletescript to "rm "
repeat with thenewfile in files_to_process
set mynewfullpath to "'" & filepath1POSIX & "/" & thenewfile & "'" & space
set deletescript to deletescript & filepath1POSIX & "/" & thenewfile & space
end repeat
do shell script deletescript
set combinedPDF_alias to (path to desktop as string) & "Export_Objects:temp:" & search_string & "-" & "combined.pdf" as alias
tell application "Finder" to move combinedPDF_alias to CombinedPDFfolder with replacing
end Combine_PDFs
Guys I know this is an old thread, but I have the same issue. I’m using an Apple Shortcut to run the following Apple Script:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "Terminal"
set f to not (running)
activate
if f then delay 1
if (count windows) = 0 then
do script "wakeonlan -i 192.168.1.aaa 3c:a6:f6:8f:yy:xx"
else
do script "wakeonlan -i 192.168.1.aaa 3c:a6:f6:8f:yy:xx" in tab 1 of window 1
end if
end tell
tell application id "com.apple.Terminal" to close the front window
Which works fine, but I do have the dialog box popping up asking me if I went to terminate the terminal program.
If there a way of running it and avoiding that dialog box? Or even better running it without using the Terminal app at all, so that it runs invisibly?
which provides the full path to the specified command. It should return something like this:
/usr/local/bin/wakeonlan
Use that instead of just the command name in a do shell script, something like this:
do shell script "/usr/local/bin/wakeonlan -i 192.168.1.aaa 3c:a6:f6:8f:yy:xx
You can probably replace the entire ‘tell terminal’ block with the one line. ‘do shell script’ doesn’t use the same environment that the terminal does; it is much more restrictive. So using any commands that you’ve installed will require their full path.
Hi dbrewood
I’m not very confident in my English skills, so I might have misunderstood your intention, dbrewood. However, I believe that if you execute exit, the dialog will not appear when closing.
tell application "Terminal"
activate
do script "exit" in front tab of front window
#wait exit
delay 1
tell front window to close
end tell
If the dialog still appears when using exit, you might want to try sending a Ctrl+C command using a system event, and then execute exit.
Many thanks! I just tried that, but the Terminal window stays open and I get error:
error "Terminal got an error: Can’t get tab 1 of window 1." number -1728 from tab 1 of window 1
The full script I’m running is:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "Terminal"
set f to not (running)
activate
if f then delay 1
if (count windows) = 0 then
do script "wakeonlan -i 192.168.1.255 3c:a6:f6:8f:xx:yy"
else
do script "wakeonlan -i 192.168.1.255 3c:a6:f6:8f:xx:yy" in tab 1 of window 1
end if
end tell
tell application "Terminal"
activate
do script "exit" in front tab of front window
#magic 1 sec for wait exit
delay 1
tell front window to close
end tell