Quitting a script not working

i wrote this script (it tells the size of pdf files to see if they can be easily emailed and if just one large pdf file is dropped, the script will split it to parts that should be small enough to email separately).

and it works really well for me generally, but the shell script command (which i picked up here at macscripters) to quit the script-app does not always work. i want the script to give a MB “weight” for any files dropped on it; if there is just one big file, the script should continue and split the big pdf into smaller files, else (if there are several files) i want the script to quit.

first, i saved the script as an app on my desktop and dropped files were processed correctly. then, because i like to just keep aliases on my desktop, i moved the script to my applescript folder and put an alias to it on my desktop). with the script not on the desktop, the shell script command to quit the script-app does not work.

any ideas??


(*PDF Mail Scale*)

on open theList
	
	try
		
		(*
		this first part gives the combined size of several files
		*)
		
		tell application "Finder"
			
			set totalSize to 0
			set counter to 0
			
			
			repeat with x in theList
				
				set mbSize to ((totalSize + (size of x)) / 1048576)
				set round_num to round of mbSize rounding down
				
				set totalSize to (totalSize + (size of x))
				
				set counter to counter + 1
				
			end repeat
			
			if totalSize < (2281835 * 1) then display dialog "� the number of files dropped: " & (counter as string) & " � the total size is about " & round_num & " MB" & " or " & totalSize & " bytes" & " � so, this set should be ok to email �"
			if totalSize = (2281835 * 1) then display dialog "� the number of files dropped: " & (counter as string) & " � the total size is about " & round_num & " MB" & "� so, this set should be ok to email �"
			if totalSize > (2281835 * 1) then display dialog "� the number of files dropped: " & (counter as string) & " � the total size is about " & round_num & " MB" & "� so, this set is too big to email �"
			
			
			tell application "PDF Mail Scale"
				activate
			end tell
			
			set app_name to "PDF Mail Scale"
			
			if counter > 1 then set the_PID to (do shell script "ps ax | grep " & (quoted form of app_name) & " | grep -v grep | awk '{print $1}'")
			if counter > 1 then if the_PID is not "" then do shell script ("kill -9 " & the_PID)
			
		end tell
	end try
	
	try
		
		set app_name to "PDF Mail Scale"
		
		if counter > 1 then set the_PID to (do shell script "ps ax | grep " & (quoted form of app_name) & " | grep -v grep | awk '{print $1}'")
		if counter > 1 then if the_PID is not "" then do shell script ("kill -9 " & the_PID)
		
		
		(*		
		the shell script above will kill the app if there was a group of files dropped on the PDF Mail Scale app; this next part gives the size of a single large file and then calculates how many pieces to split it into to email and then splits it
		*)
		
		-- if the pdfs are about 2.1 MB or (2281835 * 1), they should be easily sent via email
		
		
		
		set size_check1 to totalSize
		set MB_of_check1 to (totalSize / 1048576)
		set round_num to round of MB_of_check1 rounding down
		
		if size_check1 < (2281835 * 1) then display dialog "� the size of this file is is not over 2.1 MB � so, it should be ok to email �"
		if size_check1 = (2281835 * 1) then display dialog "� the size of this file is is not over 2.1 MB � so, it should be ok to email �"
		
		-- if the pdfs are bigger than 2.1 MB, that is " if size_check1 > (2281835 * 1) ", they should be split into sections of about 2 MB each,  that is " ({size_check1 / 1048576} / 2) " ; arts splitter needs to be told how many pages it should make each of the fragments
		
		-- so, find the total MB of the file, that is " {size_check1 / 1048576} & " MB" "; divide that total MB by 2 MB, that is " {size_check1 / 1048576} / 2) ", and get the number of fragments so each fragment will be about 2 MB
		
		-- set size_check1 to size of (info for theFile) and {size_check1 / 1048576}  gets the size of the file in MB
		
		set part_num to {size_check1 / 1048576} / 2
		set part_num to round of part_num rounding up
		set part_num1 to (part_num + 1)
		
		-- then, get the total page count of the pdf; divide the total page count by the number of fragments and get the number of pages in each fragment 
		
		set combined_page_count to 0
		if size_check1 > (2281835 * 1) then tell application "Acrobat 5.0" to open x
		if size_check1 > (2281835 * 1) then tell application "Acrobat 5.0" to set combined_page_count to (count PDPage of document 1)
		
		
		(*	set frag_num to combined_page_count / ({size_check1 / 1048576} / 2)
		set frag_num to round of frag_num rounding up*)
		
		set frag_num to combined_page_count / part_num
		set frag_num to round of frag_num rounding up
		
		set frag_num1 to combined_page_count / part_num1
		set frag_num1 to round of frag_num1 rounding up
		
		-- then, display the message
		
		tell application "PDF Mail Scale"
			activate
		end tell
		
		if size_check1 > (2281835 * 1) then display dialog "� the size of this file is " & round_num & " MB" & "  �  so, this script will split it into about " & part_num & " parts" & "  �  the total page count is " & combined_page_count & "  �  so, that's into fragments of " & frag_num & " pages or so �  if this does not work, try fragments of " & frag_num1 & " pages"
		
		
		set file_name to name of (info for x) -- gets name of the pdf
		
		
		with timeout of 1200 seconds
			if size_check1 > (2281835 * 1) then tell application "Acrobat 5.0"
				activate
			end tell
			tell application "System Events"
				tell process "Acrobat 5.0"
					tell menu bar 1 -- menu bar 
						tell menu "Plug-Ins" -- menu 
							tell menu item "ARTS PDF Splitter" -- menu item 
								tell menu 1 -- sub-menu 
									click menu item "Split this file into groups" -- sub-menu item
									delay 3
									keystroke (file_name as string) & "_"
									delay 3
									keystroke return
									delay 3
									keystroke (frag_num as string)
									delay 1
									keystroke return
								end tell
							end tell
						end tell
					end tell
				end tell
			end tell
		end timeout
		
		
		
	end try
end open


If you need a good suicide, take a look to this one:
http://macscripter.net/exchange/index.php?id=P103

However, for your code I don’t think you need a kill, but a simple “return” statement, which means “don’t go ahead”. Try substituting your shell stuff with “return”. For example:

set x to 1
set x to 2

return

set x to 3

In this sample code, “x” will never will reach the value “3”.

“return” usually means a return to the latest order. I think this should work for your script. For others, you could use a silent error, such as “error number -128” (user cancelled), which will abort quietly the process.
Calls to the “kill” command are useful to abort processes which you can’t quit using other methods (via apple events), such as those ones running in a shell (eg, “cat” or “find”).

(apologies if this turns out to be a double post)

return seems ot be working. it’s a cleaner script.

thanks

(i’ve saved your post for reference, too)

Haven’t had time to look at your code in depth, but if you were looking for a way to quit the app without looking at the sizes first (since you want it to quit if there is one big file). I’d write this code so that it contained two arrays… One with file names and the number of bytes. If there is only one file and the number of bytes are over X, then quit.

You may have to activate the app (to bring it to the front) before quitting. Otherwise the QUIT command may time out if the app is still doing other stuff in the background.