Script advice needed…

Through the help of you guys here… I have cobbled together code into a script that works somewhat well, but I’d like to refine it a few ways. This is where I’m lost. This script basically:

  1. asks user to choose folder
  2. checks for any PDF files… if they exist a dialog is displayed telling the user to convert the files to EPS (Ths user does this manually after ending the script)
  3. It checks for filename length… anything over 21 chracters and the user gets a warning dialog, then a dialog in which to enter the new name. This works great as is.

The thing is, in use - it could be a bit confusing for the user. When the script finds a PDF, the user is warned and it simply moves on to the next file/task. If it happens to find a file longer than 21 characters, then the user gets the “rename” part of the script. The way it works though, from the user standpoint, they run the script initially, it gives them any number of dialogs, depending on what it finds. Upon the script ending, the user should then convert any PDF files it found, and re-run the script. So in practice, they run the script a few times, fix what it says and they’re done.

What I’d like to do is add a clause that will display a dialog that all the files are good - if nothing is found to be wrong in the first place. How can I add this “completion” dialog under the condition that none of the files have any problems, and don’t trigger any of the aforementioned PDF, or 21 character dialog within the script.

Thanks in advance, here is my script so far:

set myfolder to (choose folder)
checkNames(myfolder)

on checkNames(theFolder)
	set maxLen to 21
	tell application "Finder"
		set fileNames to name of every file of theFolder
		repeat with thisName in fileNames
			set ASTID to AppleScript's text item delimiters
			set AppleScript's text item delimiters to "."
			set nameExtn to "." & thisName's text item -1
			if nameExtn is ".pdf" or ".PDF" then
				display dialog "Classpage can't use PDF fillers, please convert this file to EPS... then re-run this script when ready." & return & return & thisName & return buttons {"Continue..."}
			else
				if (thisName's length > maxLen) then
					display dialog "This file is too long:" & return & return & thisName & return & return & "Let's rename the file so CLASSPAGE doesn't crash..." buttons {"Cancel", "Let's do this..."} default button 2
					if the button returned of the result is "" then
					else
						display dialog "Enter the new file name for:" & return & return & thisName & return & return & "Do NOT go over 17-characters!" & return & "Do NOT include the .EPS extension" & return default answer "" buttons {"OK"} default button 1
						set newName to text returned of the result
						set name of file (thisName as text) of folder theFolder to newName & nameExtn
					end if
				end if
			end if
		end repeat
	end tell
end checkNames
end

Does this work for you?

choose folder with prompt "Check files in this folder:"
checkNames(result)

on checkNames(theFolder)
	tell application "Finder"
		activate
		
		try
			-- This throw an error when there are no PDF files.
			set pdfNames to name of files of theFolder whose name extension is "pdf"
			
			set ASTID to AppleScript's text item delimiters
			set AppleScript's text item delimiters to {ASCII character 13}
			set pdfNames to pdfNames as Unicode text
			set AppleScript's text item delimiters to ASTID
			
			display dialog "Classpage can't use PDF fillers. Please convert the following files to EPS, then re-run this script." & return & return & pdfNames buttons {"Cancel"} default button 1
		on error errMsg number errNum
			-- Handle the dialog's cancel button
			if errNum is -128 then error errMsg number errNum
		end try
		
		try
			set longNames to name of files of theFolder whose name's length > 21
			
			set ASTID to AppleScript's text item delimiters
			set AppleScript's text item delimiters to {ASCII character 13}
			set longNames to longNames as Unicode text
			set AppleScript's text item delimiters to ASTID
			
			display dialog "Let's rename these files so Classpage doesn't crash:" & return & return & longNames buttons {"Cancel"} default button 1
		on error errMsg number errNum
			-- Handle the dialog's cancel button
			if errNum is -128 then error errMsg number errNum
		end try
		
		display dialog "All files in this folder are ready to be processed by Classpage." buttons {"OK"} default button 1
	end tell
end checkNames

Wow thanks Bruce. :slight_smile:

I tested the script, and it works for the most part, and I like how it treats the PDF’s. However, it doesn’t seem to trigger anything when files are > 21 characters. I’ll probably try messing with it a bit, but chance are I’ll mess it up.

Use this instead:

choose folder with prompt "Check files in this folder:"
checkNames(result)

on checkNames(theFolder)
	tell application "Finder"
		activate
		
		try
			-- This throw an error when there are no PDF files.
			set pdfNames to name of files of theFolder whose name extension is "pdf"
			
			set ASTID to AppleScript's text item delimiters
			set AppleScript's text item delimiters to {ASCII character 13}
			set pdfNames to pdfNames as Unicode text
			set AppleScript's text item delimiters to ASTID
			
			display dialog "Classpage can't use PDF fillers. Please convert the following files to EPS, then re-run this script." & return & return & pdfNames buttons {"Cancel"} default button 1
		on error errMsg number errNum
			-- Handle the dialog's cancel button
			if errNum is -128 then error errMsg number errNum
		end try
		
		set longNameList to {}
		set epsNames to name of files of theFolder
		
		repeat with thisName in epsNames
			if thisName's length > 21 then set longNameList's end to thisName
		end repeat
		
		if longNameList is not {} then
			set ASTID to AppleScript's text item delimiters
			set AppleScript's text item delimiters to {ASCII character 13}
			set longNameList to longNameList as Unicode text
			set AppleScript's text item delimiters to ASTID
			
			display dialog "Let's rename these files so Classpage doesn't crash:" & return & return & longNameList buttons {"Cancel"} default button 1
		end if
		
		display dialog "All files in this folder are ready to be processed by Classpage." buttons {"OK"} default button 1
	end tell
end checkNames

Thanks for your help Bruce, I really appreciate it. Your amendments are working well, but what I miss about the original script is that when a file is longer than 21 characters, the dialog will mention the file and present a button to rename the file (“Let’s do this”) then another dialog with a text field pops up, and the user can rename the file right there - which I found convenient - for the user. As it is now, the script has no renaming capability on it’s own, forcing the user to manually go to the file and rename it. I’d like to try to incorporate this previous functionality, but into the framework of your script.

Thanks again, you’re obviously a pro. :cool:

I have tested this as much as I can. It seems to work well.
This script will check for pdf’s convert a copy of them to Postscript files ( which I think eps file are )
Move the originals out of the folder.
Then check file name length.

You will need to change where the original files get moved to in this line.

do shell script “mv " & “"” & thePOSIXFileName & “"” & " ~/Desktop/” & “""”

Here they get moved to the desktop.

Also check what max length number I changed it to 18.
The applescript I hacked to bits can be found here.
/Library/Scripts/Printing Scripts/

(*

Mark Hunte, awacht, Bruce Phillips 2006, Apple Computers 2003
 Some of this code was descended from Apple sample
code, and I have made changes to it. It also contains code found here.
http://bbs.applescript.net/viewtopic.php?id=16941
*)

--property RadarProblem3258323IsFixed : false
property type_list : {"PDF"}
property extension_list : {"pdf"}
--html is not currently handled

on run {}
	tell application "Finder"
		choose folder with prompt "Check files in this folder:"
		set theFolder to result
		set pdfNames to every item of theFolder whose name extension is "pdf"
		
	end tell
	--
	
	if pdfNames is not {} then
		
		
		tell application "Finder"
			display dialog "Classpage can't use PDF fillers. " & return & ¬
				"Please Click OK to convert the files to EPS, then re-run this script." buttons {"Cancel", "Ok"} default button 2
		end tell
		if the button returned of the result is "Ok" then
			--
			
			set FS to {}
			repeat with EachItem in pdfNames
				copy EachItem as alias to end of FS
			end repeat
			
			
			set SelectionCount to number of FS -- count	
			
			open FS
		end if
		
	end if
	my checkNames(theFolder)
end run



on open these_items
	set thesefiles to {}
	set the item_info to {}
	repeat with i from 1 to the count of these_items
		set this_item to (item i of these_items)
		set the item_info to info for this_item
		
		if folder of the item_info is true then --if the item is a folder
			processFolder(this_item)
		else if ((folder of the item_info is false) and ¬
			(alias of the item_info is false)) and ¬
			(the file type of the item_info is in the type_list) or ¬
			((the name extension of the item_info) is in the extension_list) then
			
			set theFilePath to (item i of these_items as string)
			set thePOSIXFilePath to POSIX path of theFilePath as string
			processFile(thePOSIXFilePath)
		end if
	end repeat
	
end open

--process folders 
on processFolder(theFolder)
	set these_items to list folder theFolder without invisibles
	repeat with i from 1 to the count of these_items
		set this_item to alias ((theFolder as text) & (item i of these_items))
		set the item_info to info for this_item
		if folder of the item_info is true then
			processFolder(this_item)
		else if (alias of the item_info is false) and ¬
			((the file type of the item_info is in the type_list) or ¬
				the name extension of the item_info is in the extension_list) then
			set theFilePath to (this_item as string)
			set thePOSIXFilePath to POSIX path of theFilePath as string
			processFile(thePOSIXFilePath)
		end if
	end repeat
end processFolder

--need to pass the URL to Terminal
on processFile(thePOSIXFileName)
	try
		set terminalCommand to ""
		set convertCommand to "/System/Library/Printers/Libraries/./convert "
		set newFileName to thePOSIXFileName & ".ps"
		set terminalCommand to convertCommand & "-f " & "\"" & thePOSIXFileName & "\"" & " -o " & "\"" & newFileName & "\"" & " -j \"application/postscript\""
		
		do shell script terminalCommand
		do shell script "mv " & "\"" & thePOSIXFileName & "\"" & " ~/Desktop/" & "\"\""
	end try
end processFile
on checkNames(theFolder)
	set maxLen to 18 --  you may need to change this
	tell application "Finder"
		set fileNames to name of every file of theFolder
		repeat with thisName in fileNames
			set ASTID to AppleScript's text item delimiters
			set AppleScript's text item delimiters to "."
			set nameExtn to "." & thisName's text item -1
			set AppleScript's text item delimiters to ""
			if (thisName's length > maxLen) then
				display dialog "This file is too long:" & return & return & thisName & return & return & "Let's rename the file so CLASSPAGE doesn't crash..." buttons {"Cancel", "Let's do this..."} default button 2
				if the button returned of the result is "" then
				else
					set newName to thisName
					repeat until newName's length is less than maxLen
						
						display dialog "Enter the new file name for:" & return & return & thisName & return & return & "Do NOT go over 17-characters!" & return & "Do NOT include the .EPS extension" & return default answer "" buttons {"OK"} default button 1
						set newName to text returned of the result
					end repeat
					set name of file (thisName as text) of folder theFolder to newName & nameExtn
					
				end if
			end if
			
		end repeat
		
		tell application "Finder"
			display dialog "All Done :" buttons {"Ok"} default button 1
		end tell
	end tell
end checkNames