Applescript- File Extension Condition placement

Discussion in 'Mac Programming' started by DerekStiles, Jul 12, 2011.

  1. DerekStiles, Jul 12, 2011
    Last edited by a moderator: Jul 12, 2011

    DerekStiles macrumors newbie

    Joined:
    Jul 12, 2009
    #1
    So this is first script I've tried so far, the objective of which is to set up a drag and drop folder that takes info from an excel workbook, copies it into an illustrator document, and then saves a pdf of it. The problem I'm running into now is how to make sure it runs only on excel files. I'm thinking the extension condition needs to be under an application tell (no logic behind that really, just how I've seen it everywhere else), but then it seem I would have to place that condition under every tell. Seems like there should be a better way. Any help would be greatly appreciated!
    Code:
    property application_extension_list : {"xlsx", "xls"}
    on adding folder items to this_folder after receiving added_items
    	try
    		repeat with i from 1 to number of items in added_items
    			if (the name extension of the (item i of added_items) is in the application_extension_list) then
    				tell application "Microsoft Excel"
    					activate
    					open item i of added_items
    					copy range (range "A3")
    					close active workbook saving no
    				end tell
    				set fp to this_folder & ":Output:" & (the clipboard) & ".pdf" as string
    				tell application "Adobe Illustrator"
    					activate
    					open "/Users/karen_r/Desktop/TestDragDrop/Resources/testtemp.ai"
    					set (contents of words of every line of every text frame in document 1 whose contents is "name") to the clipboard
    					save current document in file fp as pdf ¬
    						with options {class:PDF save options ¬
    						, compatibility:Acrobat 5 ¬
    						, preserve editability:true}
    					close current document saving no
    				end tell
    				tell application "Finder"
    					activate
    					duplicate item i of added_items to folder ("mac hd:Users:karen_r:Desktop:TestDragDrop:Output" as alias) with replacing
    					delete item i of added_items
    				end tell
    			end if
    		end repeat
    	on error
    		return
    	end try
    	
    end adding folder items to
    
     
  2. DerekStiles, Jul 13, 2011
    Last edited: Jul 13, 2011

    DerekStiles thread starter macrumors newbie

    Joined:
    Jul 12, 2009
    #2
    okay, so i found a solution that works (and cleaned up my code a bit as well I think)
    Code:
    property application_extension_list : {"xlsx", "xls"}
    on adding folder items to this_folder after receiving added_items
    	tell application "Finder"
    		set a_list to every file of this_folder
    	end tell
    	repeat with i from 1 to number of items in a_list
    		try
    			set currentitem to (item i of a_list)
    			if (the name extension of the currentitem is in application_extension_list) then
    				tell application "Microsoft Excel"
    					activate
    					open item i of a_list
    					copy range (range "A3")
    					close active workbook saving no
    				end tell
    				set fp to "mac hd:Users:karen_r:Desktop:TestDragDrop:Output:" & (the clipboard) & ".pdf" as string
    				tell application "Adobe Illustrator"
    					activate
    					open "Mac HD:Users:karen_r:Desktop:TestDragDrop:Resources:testtemp.ai" as alias
    					set (contents of words of every line of every text frame in document 1 whose contents is "name") to the clipboard
    					save current document in file fp as pdf ¬
    						with options {class:PDF save options ¬
    						, compatibility:Acrobat 5 ¬
    						, preserve editability:true}
    					close current document saving no
    				end tell
    				tell application "Finder"
    					activate
    					duplicate item i of a_list to folder ("mac hd:Users:karen_r:Desktop:TestDragDrop:Output" as alias) with replacing
    					delete item i of a_list
    				end tell
    			end if
    		on error the error_message number the error_number
    			display dialog "Folder action caused an error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
    		end try
    		end repeat
    end adding folder items to
    
    so I think the first had a problem in that "added_items" couldn't be used in the "if, then" statement, nor in setting the variable "currentitem". It seems that maybe it is because the value is a string, but then I don't understand why it worked in some of my code previously (it worked in the duplicate and delete commands). Any ideas?
     

Share This Page