Applescript to Service (Automator) Help needed.

Discussion in 'Mac Programming' started by VideoBeagle, Sep 7, 2014.

  1. VideoBeagle macrumors 6502a

    Joined:
    Aug 17, 2010
    Location:
    App Q&A testing by request.
    #1
    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
     

    Attached Files:

  2. kryten2, Sep 7, 2014
    Last edited: Sep 7, 2014

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #2
    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
     
  3. VideoBeagle, Sep 7, 2014
    Last edited: Sep 7, 2014

    VideoBeagle thread starter macrumors 6502a

    Joined:
    Aug 17, 2010
    Location:
    App Q&A testing by request.
    #3
    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..
     
  4. VideoBeagle thread starter macrumors 6502a

    Joined:
    Aug 17, 2010
    Location:
    App Q&A testing by request.
    #4
    And FIXED WOO!!!!
    Changing the above line to this:
    Code:
    set mifold to (the first item of the input)
    And it's working!
     

Share This Page