Close a Terminal window without dialog

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 :stuck_out_tongue:

Hi,

is the normal do shell script way not working?

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

I think so :slight_smile:

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 :smiley:

--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