Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

VideoBeagle

macrumors 6502a
Original poster
Aug 17, 2010
822
18
App Q&A testing by request.
I put together a script that goes into a folder and names the images inside based on the folders those images are in.

I would like to make it a finder service so I can right click a folder to use it.
(I've done these before, so I know, in theory, how to do it)

When I try to use it, or more to the point, Compile the applescript in it, I get a Syntax Error: Expected “end” but found “to”.

The "to" is the subroutine of the script...so does Automater/Services not know how to do use it? (Again, the Script itself works fine.)

Anyone have ideas or help in this?

THE ORIGINAL SCRIPT:
Code:
(* This is an applescript that takes images in a folder and renames them based on the name of the folder. It will transverse thru subfolders and do the same.*)

set all_files to {}
set filelist to {}
set foldlist to {}
set ImgExtension to {"jpg", "psd", "gif", "png"}

tell application "Finder"
	set mifold to choose folder
	try
		set filelist to (every file of mifold where name extension is in ImgExtension)
		set foldlist to every folder of mifold #as list
	end try
	if foldlist is not {} then
		repeat with eachFolder in foldlist
			--set Base_Name to my MakeBase(eachFolder as string)
			set count_er to 1
			set all_files to (get every document file in eachFolder where name extension is in ImgExtension)
			set finalNamePrefix to (name of (eachFolder) & "_")
			repeat with thisFile in all_files
				--set thisFile's name to (Base_Name & (text -3 thru -1 of ("000" & (count_er as string))) & "." & (thisFile's name extension))
				set thisFile's name to (finalNamePrefix & (text -3 thru -1 of ("000" & (count_er as string))) & "." & (thisFile's name extension))
				set count_er to count_er + 1
			end repeat
			
		end repeat
	end if
	if filelist is not {} then
		set count_er to 1
		#set all_files to (get every document file in eachFolder)
		set finalNamePrefix to (name of (mifold) & "_")
		repeat with thisFile in filelist
			--set thisFile's name to (Base_Name & (text -3 thru -1 of ("000" & (count_er as string))) & "." & (thisFile's name extension))
			set thisFile's name to (finalNamePrefix & (text -3 thru -1 of ("000" & (count_er as string))) & "." & (thisFile's name extension))
			set count_er to count_er + 1
		end repeat
	end if
	beep 3
	
end tell

to MakeBase(txt)
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ":"
	set new_Name_Raw to every text item of txt
	set AppleScript's text item delimiters to "_"
	set final_Name to every text item of new_Name_Raw as text
	set AppleScript's text item delimiters to astid
	return final_Name
end MakeBase

THE SCRIPT MODIFIED FOR THE SERVICE:
Code:
on run {input, parameters}
set all_files to {}
set filelist to {}
set foldlist to {}
set ImgExtension to {"jpg", "psd", "gif", "png"}

tell application "Finder"
	set mifold to input
	try
		set filelist to (every file of mifold where name extension is in ImgExtension)
		set foldlist to every folder of mifold #as list
	end try
	if foldlist is not {} then
		repeat with eachFolder in foldlist
			--set Base_Name to my MakeBase(eachFolder as string)
			set count_er to 1
			set all_files to (get every document file in eachFolder where name extension is in ImgExtension)
			set finalNamePrefix to (name of (eachFolder) & "_")
			repeat with thisFile in all_files
				--set thisFile's name to (Base_Name & (text -3 thru -1 of ("000" & (count_er as string))) & "." & (thisFile's name extension))
				set thisFile's name to (finalNamePrefix & (text -3 thru -1 of ("000" & (count_er as string))) & "." & (thisFile's name extension))
				set count_er to count_er + 1
			end repeat
			
		end repeat
	end if
	if filelist is not {} then
		set count_er to 1
		#set all_files to (get every document file in eachFolder)
		set finalNamePrefix to (name of (mifold) & "_")
		repeat with thisFile in filelist
			--set thisFile's name to (Base_Name & (text -3 thru -1 of ("000" & (count_er as string))) & "." & (thisFile's name extension))
			set thisFile's name to (finalNamePrefix & (text -3 thru -1 of ("000" & (count_er as string))) & "." & (thisFile's name extension))
			set count_er to count_er + 1
		end repeat
	end if
	beep 3
	
end tell

to MakeBase(txt)
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ":"
	set new_Name_Raw to every text item of txt
	set AppleScript's text item delimiters to "_"
	set final_Name to every text item of new_Name_Raw as text
	set AppleScript's text item delimiters to astid
	return final_Name
end MakeBase


end run
 

Attachments

  • Screenshot 2014-09-07 19.58.01.png
    Screenshot 2014-09-07 19.58.01.png
    202.9 KB · Views: 108

kryten2

macrumors 65816
Mar 17, 2012
1,114
99
Belgium
Move your MakeBase handler out of the on run handler e.g.

Code:
...
end run

to MakeBase(txt)
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ":"
	set new_Name_Raw to every text item of txt
	set AppleScript's text item delimiters to "_"
	set final_Name to every text item of new_Name_Raw as text
	set AppleScript's text item delimiters to astid
	return final_Name
end MakeBase

Look at the section about run handlers here
 
Last edited:

VideoBeagle

macrumors 6502a
Original poster
Aug 17, 2010
822
18
App Q&A testing by request.
Thanks as always Kryten2...I didn't even know the right words to find an answer googling (bad error message :( ) slowly but surely I'm getting the hang of this all:)

---
And the code just doesn't work..argh.
Ok..now to figure out where it doesn't work.

---
Seems to be here....
Code:
on run {input, parameters}
	set mifold to input
not picking up the folder....the regular script doesn't need to be converted to posix, so not sure why this is treated differently..
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.