image automation

Discussion in 'Mac Programming' started by perroni, Mar 8, 2011.

  1. perroni, Mar 8, 2011
    Last edited by a moderator: Mar 8, 2011

    perroni macrumors newbie

    Joined:
    Mar 8, 2011
    #1
    To Whom It May Concern:
    I’m pretty much at my wits end with this script. I’m simply trying to get a folder to react when a file is written to it. That’s all. As you will see in the script below, I’ve tried a number of different logic and nothing wants to play nice. I can get it to work if I manually select the image. That’s counter productive when I will be running 1000 images through this process on any given afternoon. To test this, I created a folder named Train that contains two folders inside it, a-before and b-after. All I need is when an image is written to a-before, it opens the image and manipulates it based on the actions in the script, (scale , pad and move to b-after). This works with manually selecting the image as shown. Any help would be appreciated.



    Code:
    tell application "Finder" to set this_file to choose file without invisibles
    tell application "Image Events"
    	launch
    	set this_image to open this_file
    	set the target_length to 800
    	scale this_image to size target_length
    	pad this_image to dimensions {800, 800}
    	save this_image with icon
    	close this_image
    	tell application "Finder"
    		move every file of folder "Mac HD:Users:facianp:Desktop:train:a-before:" to folder "Mac HD:Users:facianp:Desktop:train:b-after:"
    	end tell
    end tell
    
    Below are different scripts I’ve written that have failed.
    
    --tell application "Finder" to set this_image to every file of folder "Mac HD:Users:facianp:Desktop:train:a-before:"
    
    --tell application "Finder" to get every file of folder "Mac HD:Users:facianp:Desktop:train:a-before"
    
    --tell application "System Events" to get items of folder "Mac HD:Users:facianp:Desktop:train:a-before:"
    
    --tell application "Finder" to set this_image to every file of folder "Mac HD:Users:facianp:Desktop:train:a-before:" as alias
    
    (*tell application "Finder"
    	set this_file to every file of folder "Mac HD:Users:facianp:Desktop:train:a-before:"
    end tell*)
    
    --tell application "Finder" to set theFolder to "Mac HD:Users:facianp:Desktop:train:a-before:"
    
    --on adding folder items to theFolder after receiving this_file
    
    (*try
    	set theFolder to "Mac HD:Users:facianp:Desktop:train:a-before:" as alias
    	tell application "System Events" to set this_image to every file of theFolder
    end try*)
     
  2. McGordon, Mar 8, 2011
    Last edited: Mar 8, 2011

    McGordon macrumors member

    Joined:
    Dec 28, 2010
    Location:
    Scotland
    #2
    Firstly, put your code in a code block when you post it on here, it makes it a lot easier to read.

    This will convert each file in your Desktop:train:a-before folder, then move them to Desktop:train:b-after folder. The script will need to be run every time you want to process a batch, but if you're doing 1000 at a time, that's not a big deal. Rewriting it as a folder action would be nice. You can do that.

    Code:
    set aBefore to ((path to desktop) & "train:a-before" as text) as alias
    set bAfter to ((path to desktop) & "train:b-after" as text) as alias
    tell application "System Events"
       set allImages to every disk item in aBefore whose visible is true
    end tell
    
    tell application "Image Events"
       launch
       repeat with this_file in allImages
          set this_image to open this_file as alias
          set the target_length to 800
          scale this_image to size target_length
          pad this_image to dimensions {800, 800}
          save this_image with icon
          close this_image
          move this_file to bAfter
       end repeat
    end tell
    

    Edit:
    Here's a folder action script. Save it in
    ~/Library/Scripts/Folder Action Scripts

    Then attach it by right-clicking on the folder you want to use it with, then select "Folder Actions Setup...". Choose the script and enable folder actions in the check box.

    Now just drag images to that folder and they'll be converted and moved to the folder inside it.

    Code:
    on adding folder items to this_folder after receiving new_files
       tell application "Finder"
          if not (exists folder "Scaled & Padded" of this_folder) then
             make new folder at this_folder with properties {name:"Scaled & Padded"}
          end if
          set the afterFolder to folder "Scaled & Padded" of this_folder as alias
       end tell
       
       tell application "Image Events"
          launch
          repeat with this_file in new_files
             if this_file is not the afterFolder then
                set this_image to open this_file as alias
                set the target_length to 800
                scale this_image to size target_length
                pad this_image to dimensions {800, 800}
                save this_image with icon
                close this_image
                move this_file to afterFolder
             end if
          end repeat
       end tell
    end adding folder items to
    
    
     

Share This Page