Modifying this script to choose more than one folder at a time

Hello,

I found this script here, and I am testing it to print a bunch of InDeign files at one time.

Right now, I can pick one Folder at a time, and it will only find the the .indd file in the top tier of that folder .

I have two questions/thoughts.

1-Can this script be modified to choose more than one folder at a time? (I have looked at the AS guide and have found that I can open multiple selections…but when I place that in the code it doesn’t work.
2-Can this script be modified to choose subfolders and look for only the .indd files?

on choosePrinter()
	tell application "Printer Setup Utility"
		set printerList to name of every printer
	end tell
	choose from list printerList with prompt "Choose the printer you wish to use."
	set thePrinter to item 1 of result
	
	tell application "Printer Setup Utility"
		set current printer to first printer whose name is thePrinter
	end tell
end choosePrinter

property scriptVersion : "0.3"
property theScriptDesc : "InDesign Print" & return & ""
display alert (theScriptDesc as string) message ("Version " & (scriptVersion as string)) giving up after 2 buttons "¢"

choosePrinter()
set FileSample to choose folder with prompt "Choose an input folder."
set folderName to name of (info for FileSample)

tell application "Finder" to set theFiles to files of folder FileSample whose name extension is in {"indd"} or kind contains "InDesign"

repeat with oneFile in theFiles
	set oneFileName to name of oneFile
	tell application "Adobe InDesign CS5.5"
		activate
		open (oneFile as alias)
		tell front document
			tell print preferences
				set page range to all pages
				set scale proportional to true
				set scale width to 100
				set page position to centered
				--set mark offset to 0.0833 --as real?
				set page information marks to true
			end tell
			print without print dialog
			close saving no
		end tell
	end tell
end repeat

tell application "Finder"
	activate
	beep
	display dialog "All input files have been printed." buttons "Ok" default button 1
end tell

thanks
babs

Hello

Choose folder is a component of Standard Additions.

This OSAX’s dictionary states :


choose folder‚v : Choose a folder on a disk or server
choose folder
[with prompt text] : the prompt to be displayed in the dialog box
[default location alias] : the default folder location
[invisibles boolean] : Show invisible files and folders? (default is false)
[multiple selections allowed boolean] : Allow multiple items to be selected? (default is false)
[showing package contents boolean] : Show the contents of packages? (Packages will be treated as folders. Default is false.)
→ alias : the chosen folder

Given that you need to edit the instruction choosing the folder(s) this way :

Original :


set FileSample to choose folder with prompt "Choose an input folder."

Edited :


set FileSamples to choose folder with prompt "Choose an input folder." with multiple selections allowed

FileSamples will be a list of path to folders.
So you will have to edit the script to loop among the list contents


repeat with oneFolder in FileSamples
# put here the code treating a single folder
end repeat 

I made the changes for you


on choosePrinter()
	tell application "Printer Setup Utility"
		set printerList to name of every printer
	end tell
	choose from list printerList with prompt "Choose the printer you wish to use."
	set thePrinter to item 1 of result
	
	tell application "Printer Setup Utility"
		set current printer to first printer whose name is thePrinter
	end tell
end choosePrinter

property scriptVersion : "0.3"
property theScriptDesc : "InDesign Print" & return & ""
display alert (theScriptDesc as string) message ("Version " & (scriptVersion as string)) giving up after 2 buttons "¢"

choosePrinter()
set FileSamples to choose folder with prompt "Choose an input folder." with multiple selections allowed
repeat with oneFolder in FileSamples
	
	# set folderName to name of (info for FileSample) # disabled because the name is never used
	# On my machine, Finder as well as System Events return kind = "indd" for inDesign documents
	# I guess that it's because inDesign isn't available here
	
	tell application "System Events" to set theFiles to (path of files of oneFolder whose name extension is "indd" or kind contains "InDesign")
	# would be better using the "type identifier" but I don't know which is the value for inDesign files.
	#tell application "System Events" to set theFiles to (path of files of folder FileSample whose type identifier  in "co.adobe.complete the indesign identifier")
	repeat with oneFile in theFiles
		# tell application "System Events" to set oneFileName to name of disk item oneFile # the original instruction was wrongly coded
		# I edited it. I disabled it because the filename was unused.
		
		# I don' own Indesign so I disabled the related code.
		(*
	tell application "Adobe InDesign CS5.5"
		activate
		open (oneFile as alias)
		tell front document
			tell print preferences
				set page range to all pages
				set scale proportional to true
				set scale width to 100
				set page position to centered
				--set mark offset to 0.0833 --as real?
				set page information marks to true
			end tell
			print without print dialog
			close saving no
		end tell
	end tell
	*)
	end repeat # with oneFile
end repeat # with oneFolder

# There is no need to trigger the Finder to display a dialog
# tell application "Finder"
#	activate
beep
display dialog "All input files have been printed." buttons "Ok" default button 1
# end tell

To treat the contents of subfolders you will have to go back to Finder and use entire contents which returns the list of every items embedded in a folder or stay with System Events and build a recursive code to scan the entire hierarchy.
It would be my preferred scheme.

KOENIG Yvan (VALLAURIS, France) dimanche 28 juillet 2013 17:25:38

Hi Yvan!

Thank you!! I will start playing with this…
much appreciated!

babs

Hello,

Well, I enabled the areas you needed to disable and tried a few more things, but I keep getting stuck on this line:

set folderName to name of (info for FileSample) # disabled because the name is never used

Here is the entire code at this point:


on choosePrinter()
	tell application "Printer Setup Utility"
		set printerList to name of every printer
	end tell
	choose from list printerList with prompt "Choose the printer you wish to use."
	set thePrinter to item 1 of result
	
	tell application "Printer Setup Utility"
		set current printer to first printer whose name is thePrinter
	end tell
end choosePrinter

property scriptVersion : "0.3"
property theScriptDesc : "InDesign Print" & return & ""
display alert (theScriptDesc as string) message ("Version " & (scriptVersion as string)) giving up after 2 buttons "¢"

choosePrinter()
set FileSamples to choose folder with prompt "Choose an input folder." with multiple selections allowed
repeat with oneFolder in FileSamples
	
	set folderName to name of (info for FileSample) # disabled because the name is never used
	--On my machine, Finder as well as System Events return kind = "indd" for inDesign documents
	--I guess that it's because inDesign isn't available here
	
	tell application "System Events" to set theFiles to (path of files of oneFolder whose name extension is "indd" or kind contains "InDesign")
	# would be better using the "type identifier" but I don't know which is the value for inDesign files.
	tell application "System Events" to set theFiles to (path of files of folder FileSample whose type identifier in "co.adobe.complete the indesign identifier")
	repeat with oneFile in theFiles
		tell application "System Events" to set oneFileName to name of disk item oneFile # the original instruction was wrongly coded
		# I edited it. I disabled it because the filename was unused.
		
		# I don' own Indesign so I disabled the related code.
		
		tell application "Adobe InDesign CS5.5"
			activate
			open (oneFile as alias)
			tell front document
				tell print preferences
					set page range to all pages
					set scale proportional to true
					set scale width to 100
					set page position to centered
					--set mark offset to 0.0833 --as real?
					set page information marks to true
				end tell
				print without print dialog
				close saving no
			end tell
		end tell
		
	end repeat # with oneFile
end repeat # with oneFolder

# There is no need to trigger the Finder to display a dialog
tell application "Finder"
	activate
	beep
	display dialog "All input files have been printed." buttons "Ok" default button 1
end tell

Any thoughts out there?
thanks
babs

(1) What need to reenable the instruction :


set folderName to name of (info for FileSample) # disabled because the name is never used

when folderName is not used by the script ?

(2) This said, I forgot to change the variable name when I edited the code replacing the varName FileSample which was meaningless by oneFolder.


set folderName to name of (info for oneFolder) # disabled because the name is never used

(3) info for is deprecated for years. You found it in a very old sample code but, at least from my point opf view, using it is a bad idea.

To get a file’s name, it’s better to use one of these two schemes.

(a)


tell application "System Events" to set folderName to name of oneFolder

(b)


text 1 thru -2 of (oneFolder as text)
	set folderName to text -((offset of ":" in ((reverse of (characters of result)) as text)) - 1) thru -1 of result

The second one is probably the fastest.

Here is the edited script.



on choosePrinter()
	tell application "Printer Setup Utility"
		set printerList to name of every printer
	end tell
	choose from list printerList with prompt "Choose the printer you wish to use."
	set thePrinter to item 1 of result
	
	tell application "Printer Setup Utility"
		set current printer to first printer whose name is thePrinter
	end tell
end choosePrinter

property scriptVersion : "0.3"
property theScriptDesc : "InDesign Print" & return & ""
#display alert (theScriptDesc as string) message ("Version " & (scriptVersion as string)) giving up after 2 buttons "¢"

#choosePrinter()
set FileSamples to choose folder with prompt "Choose an input folder." with multiple selections allowed
repeat with oneFolder in FileSamples
	(*
	text 1 thru -2 of (oneFolder as text)
	set folderName to text -((offset of ":" in ((reverse of (characters of result)) as text)) - 1) thru -1 of result # disabled because the name is never used
	*)
	--On my machine, Finder as well as System Events return kind = "indd" for inDesign documents
	--I guess that it's because inDesign isn't available here
	
	tell application "System Events" to set theFiles to (path of files of oneFolder whose name extension is "indd" or kind contains "InDesign")
	# would be better using the "type identifier" but I don't know which is the value for inDesign files.
	# tell application "System Events" to set theFiles to (path of files of oneFolder whose type identifier in "co.adobe.complete the indesign identifier")
	repeat with oneFile in theFiles
		# tell application "System Events" to set oneFileName to name of disk item oneFile # the original instruction was wrongly coded
		# I edited it. I disabled it because the filename was unused.
		
		# I don't own Indesign so I disabled the related code.
		
		tell application "Adobe InDesign CS5.5"
			activate
			open (oneFile as alias)
			tell front document
				tell print preferences
					set page range to all pages
					set scale proportional to true
					set scale width to 100
					set page position to centered
					--set mark offset to 0.0833 --as real? # no need to coerce as real, 0.0833 IS ALREADY a real !
					set page information marks to true
				end tell
				print without print dialog
				close saving no
			end tell
		end tell
		
	end repeat # with oneFile
end repeat # with oneFolder

# There is no need to trigger the Finder to display a dialog
# This is why I disabled the useless instructions !
# Don't ask for help if you refuse to take care of the given answers !
# tell application "Finder"
#	activate
beep
display dialog "All input files have been printed." buttons "Ok" default button 1
# end tell

KOENIG Yvan (VALLAURIS, France) lundi 29 juillet 2013 16:44:44

Hello…
Well…I think we are closer:

Here is the line that if I keep in-halts the script.

tell application "System Events" to set theFiles to (path of files of oneFolder whose type identifier in "co.adobe.complete the indesign identifier")

Here is what I have that goes through, but doesn’t print anything.



on choosePrinter()
	tell application "Printer Setup Utility"
		set printerList to name of every printer
	end tell
	choose from list printerList with prompt "Choose the printer you wish to use."
	set thePrinter to item 1 of result
	
	tell application "Printer Setup Utility"
		set current printer to first printer whose name is thePrinter
	end tell
end choosePrinter

property scriptVersion : "0.3"
property theScriptDesc : "InDesign Print" & return & ""
display alert (theScriptDesc as string) message ("Version " & (scriptVersion as string)) giving up after 2 buttons "¢"

choosePrinter()
set FileSamples to choose folder with prompt "Choose an input folder." with multiple selections allowed
repeat with oneFolder in FileSamples
	
	text 1 thru -2 of (oneFolder as text)
	set folderName to text -((offset of ":" in ((reverse of (characters of result)) as text)) - 1) thru -1 of result # disabled because the name is never used
	
	--On my machine, Finder as well as System Events return kind = "indd" for inDesign documents
	--I guess that it's because inDesign isn't available here
	
	tell application "System Events" to set theFiles to (path of files of oneFolder whose name extension is "indd" or kind contains "InDesign")
	# would be better using the "type identifier" but I don't know which is the value for inDesign files.
	#tell application "System Events" to set theFiles to (path of files of oneFolder whose type identifier in "co.adobe.complete the indesign identifier")
	repeat with oneFile in theFiles
		tell application "System Events" to set oneFileName to name of disk item oneFile # the original instruction was wrongly coded
		# I edited it. I disabled it because the filename was unused.
		
		# I don't own Indesign so I disabled the related code.
		
		tell application "Adobe InDesign CS5.5"
			activate
			open (oneFile as alias)
			tell front document
				tell print preferences
					set page range to all pages
					set scale proportional to true
					set scale width to 100
					set page position to centered
					--set mark offset to 0.0833 --as real? # no need to coerce as real, 0.0833 IS ALREADY a real !
					set page information marks to true
				end tell
				print without print dialog
				close saving no
			end tell
		end tell
		
	end repeat # with oneFile
end repeat # with oneFolder

# There is no need to trigger the Finder to display a dialog
# This is why I disabled the useless instructions !
# Don't ask for help if you refuse to take care of the given answers !
# tell application "Finder"
#	activate
beep
display dialog "All input files have been printed." buttons "Ok" default button 1
# end tell

thoughts? I know this is harder since you don’t have InDesign…but I appreciate all your help!!
babs

That line can’t work because it contains an explicit instruction for you to find that information. Since you don’t know it, you don’t really need this alternate method, and you can safely delete that line, as well as any code that is preceded by “” or “#”. Yvan commented out sections of the code to show that they were wrong. Just post the problem area or error message that you receive”not the entire script.

HI Marc and Yvan!!!

So sorry to you and Yvan…it is working great!!!
I accidentally messed up something in the code when I was removing some of those comments.

I can even select more than one folder within the same main folder and that’s great.

I do have a wish list question…

How difficult is it to turn this into an action that lets me drag and drop lots of folders or files onto it?

Thanks !!
Babs

Here is an edited version.
Save it as an application.
If you double click it, it will behave as the original one (applet mode)
If you drop a group of folders on its icon, it will execute the on open code (droplet mode)

As it seems that you didn’t read carefully my comments, I retyped them in shouting mode !


# I REFUSE TO CALL THE FINDER WHICH IS A SNAIL WHEN IT IS NOT ABSOLUTELY REQUIRED.
# IN FACT IT'S REQUIRED ONLY WHEN WE HAVE TO MANIPULATE ICONS OR FINDER WINDOWS.


property scriptVersion : "0.3"
property theScriptDesc : "InDesign Print" & return & ""
on run
	choose folder with prompt "Choose an input folder." with multiple selections allowed
	my mainCode(result)
end run

on open sel
	my mainCode(sel)
end open

on mainCode(FileSamples)
	# I DISABLED TWO INSTRUCTIONS TO MAKE TESTS UPON OTHER PIECES OF CODE
	# IF YOU 
	#display alert (theScriptDesc as string) message ("Version " & (scriptVersion as string)) giving up after 2 buttons "¢"
	
	#choosePrinter()
	
	repeat with oneFolder in FileSamples
		(*
	text 1 thru -2 of (oneFolder as text)
	set folderName to text -((offset of ":" in ((reverse of (characters of result)) as text)) - 1) thru -1 of result # disabled because the name is never used
	*)
		--On my machine, Finder as well as System Events return kind = "indd" for inDesign documents
		--I guess that it's because inDesign isn't available here
		
		tell application "System Events" to set theFiles to (path of files of oneFolder whose name extension is "indd" or kind contains "InDesign")
		# would be better using the "type identifier" BUT I DON'T KNOW WHICH IS THE VALUE FOR INDESIGN FILES !
		# tell application "System Events" to set theFiles to (path of files of oneFolder whose type identifier in "co.adobe.complete the indesign identifier")
		# THE TYPE IDENTIFIER USED ABOVE IS A FAKE ONE BECAUSE I DON'T KNOW THE CORRECT ONE
		# OF COURSE, ONLY ONE OF THE TWO INSTRUCTIONS CREATING THE LIST theFiles MUST BE ACTIVATED
		
		repeat with oneFile in theFiles
			# WHEN WE USE THE FINDER, THE TRAILING (DISABLED) INSTRUCTION BEHAVE WELL 
			# BECAUSE APPLESCRIPT IS FAIR ENOUGH TO SPEAK TO THE FINDER WHICH IS NOT EXPLICITELY TARGETTED BY THE CODE.
			# I DISLIKE CODE RELYING OF SUCH FEATURE.
			# AS I SPEAK TO SYSTEM EVENTS, THIS FEATURE DOESN'T APPLY. GOOD POINT FROM MY POINT OF VIEW.
			# THIS IS WHY I EDITED THE INSTRUCTION.
			# I DIDN'T THOUGHT THAT IT WAS USEFUL TO INSIST ABOUT THAT BECAUSE oneFileName IS NOT USED BY THE SCRIPT
			# set onefileName to name of oneFile # YOUR INSTRUCTION
			#
			# tell application "System Events" to set oneFileName to name of disk item oneFile # the original instruction was wrongly coded
			# I EDITED IT. I DISABLED IT BECAUSE THE FILENAME WAS UNUSED !
			#
			
			# I don't own Indesign so I disabled the related code.
			(*
		tell application "Adobe InDesign CS5.5"
			activate
			open (oneFile as alias)
			tell front document
				tell print preferences
					set page range to all pages
					set scale proportional to true
					set scale width to 100
					set page position to centered
					--set mark offset to 0.0833 --as real? # NO NEED TO COERCE AS A REAL, 0.0833 IS ALREADY A REAL !
					set page information marks to true
				end tell
				print without print dialog
				close saving no
			end tell
		end tell
		*)
		end repeat # with oneFile
	end repeat # with oneFolder
	#
	#
	# THERE IS NO NEED TO TRIGGER THE FINDER TO CALL DISPLAY DIALOG
	# THIS IS WHY I DISABLED THESE INSTRUCTIONS !
	# DON'T ASK FOR HELP IF YOU REFUSE TO TAKE CARE OF GIVEN ANSWERS !
	# tell application "Finder"
	#	activate
	#
	#
	beep
	display dialog "All input files have been printed." buttons "Ok" default button 1
	#
	# end tell
	#
end mainCode


on choosePrinter()
	tell application "Printer Setup Utility"
		set printerList to name of every printer
	end tell
	choose from list printerList with prompt "Choose the printer you wish to use."
	set thePrinter to item 1 of result
	
	tell application "Printer Setup Utility"
		set current printer to first printer whose name is thePrinter
	end tell
end choosePrinter

For the code specific to inDesign you may look at :
http://macgrunt.com/2012/08/25/indesign-scripting-lesson-24/

KOENIG Yvan (VALLAURIS, France) mardi 30 juillet 2013 10:43:12

Hi Yvan,

Thank you…and sorry for making you have to shout. :slight_smile:
Sadly, I only know how to tinker with the Finder, and I am not so good with that…but I do see your point and I am trying to understand your remarks…I really did read them…a few times actually. Some I could understand, some where a bit over my head. But, I really do appreciate you not just fixing the code to make it work, but adding all that info.
It was very helpful.
Many many many thanks!!!
Babs