Attaching code to folder that does not yet exist.

Hi, I am hoping that somebody will be able to help me. I have a folder in the Sites folder called “current_event” and it is empty. When I import photos into Aperture, they are stored in the folder “current_event” in a subfolder created by Aperture. This subfolder might be called Class1 but could have any name. I have a script attached the “current_event” folder that looks for new folders and creates two new subfolders within the newly created subfolder. For example, Aperture creates a new folder called “Class1” containing many jpg files, the script adds two new folders to Class1 called “photos” & “thumbs”.
Where I am struggling is creating a script that copies any jpg files from folder Class1 to folder “photos”, scales them to a predefined size and the copies these to folder “thumbs” and scales to another smaller size, and repeats the process when new files land in Class1 without repeating for existing files. Obviously I want all this to be fully automated when I press the import button in Aperture.

Any ideas would be much appreciated as I am completely stumped on how to attach an automator action to a folder that does not exist.

Hi mjmPhoto,

if you attach a folderaction to your current_events-folder you can use this folderaction to attach a further folderaction. This second folderaction could open / work with every picture in the new folder and save it in your subfolders …

script nr. 1 (attached to current events)


on adding folder items to this_folder after receiving these_items
    set PathtoFolderaction to "Mac Red-Grafik 3:Users:claessen:Library:Scripts:Folder Action Scripts:PhotosandThumbs.scpt" --just a samplepath to folderaction nr. 2
    repeat with a_item in these_items
        if folder of (info for a_item) is true then
            try
                tell application "Finder"
                    set Photos to make new folder at a_item with properties {name:"Photos"}
                    set Thumbs to make new folder at a_item with properties {name:"Thumbs"}
                end tell
            end try
        end if
        tell application "System Events"
            attach action to a_item using PathtoFolderaction
        end tell
    end repeat
end adding folder items to

script nr.2 (just a samplescript using image events to scale the pics)

on adding folder items to this_folder after receiving these_items
    set Photos to (this_folder as text) & "Photos"
    set Thumbs to (this_folder as text) & "Thumbs"
    
    repeat with aFile in these_items
        
        tell application "Image Events"
            activate
            set thePic to open aFile
            scale thePic to size 500
            save thePic as JPEG in file Photos
            
            scale thePic to size 50
            save thePic as JPEG in file Thumbs
            try
                close thePic
            on error
                delay 2
                close thePic
            end try
            
        end tell
    end repeat
    
    
end adding folder items to

Attention: the subfolder created by Aperture has to be empty in the moment the folderaction is attached, otherwise we’ll have to think about another solution …

Give it a try :slight_smile:

By

Hans

Hans

Thank you very much for taking the time to help me here. I have attached script nr1 to folder current-events, changing the folderaction path accordingly. I tested it and it created the two subfolders, photos & thumbs when Aperture created its folder. Unfortunately the only way Aperture will create a folder is with at least 1 image in it so I guess that is the reason the second script was not attached.:frowning:
Would it work if the one image was deleted before the instruction to attached the script? I could live with that, though not ideal.

If I attach the second script to the folder manually, then any subsequent images dropped by Aperture are copied and converted beautifully.:slight_smile:

Any ideas?

Michael

Hi Michael,

just to be sure you’ve done everything as expected:

1.) open script nr. 2 and save it in: Library:Scripts:Folder Action Scripts:
2.) open script nr. 1 and set the Mac-Path to script nr. 1 in the second line using the following script. Just open in ScriptEditor, run it, choose script nr.1 in your filesystem and copy the path from the dialogbox …

set ScriptPath to (choose file) as Unicode text
display dialog "Scriptpath" default answer ScriptPath

3.) Save it to Library:Scripts:Folder Action Scripts: and attach it to "current …

May be you’ve allready done everything in this order …

What happens¿

Expected:
The second folderaction should have been attached “ true?

If not: :confused:

Bye

Hans

Hans,

“2.) open script nr. 1 and set the Mac-Path to script nr. 1 in the second line using the following script. Just open in ScriptEditor, run it, choose script nr.1 in your filesystem and copy the path from the dialogbox …”

Did you mean set the Mac-Path to script nr 1 or 2? I set it to two and it is not being attached. I have all other paths correct and as said when attaching manually it works just fine. I tried, using another script to create a new folder in the current_event folder without any images and it still does not attach the second script to that folder. Puzzled now.

Hi Michael,

you’ve been right: I struggled in my own explanation … :stuck_out_tongue:

Well, the old scripts work on my macs (tiger). May be newer operating systems work a bit different (or more exact …).
I looked up the dictionary again, searched the forum and came up with this little change:

changed a_item from alias to text: (a_item as text) and defined it as folder: folder(a_item as text)

my fifty Cents :wink:

Hans


on adding folder items to this_folder after receiving these_items

	set PathtoFolderaction to "Macintosh HD:Users:hans:Library:Scripts:Folder Action Scripts:  script02.scpt" --still a samplaPath ;-)
	
	repeat with a_item in these_items
	
		if folder of (info for a_item) is true then
		
			try
				tell application "Finder"
					set Photos to make new folder at a_item with properties {name:"Photos"}
					set Thumbs to make new folder at a_item with properties {name:"Thumbs"}
				end tell
				
				tell application "System Events"
					attach action to folder (a_item as text) using PathtoFolderaction
				end tell
			
			end try
		end if
		
	end repeat
end adding folder items to

It seems the System Events dict in Snow Leopard does not have ¢any¢ verbs for folder actions.

‘attach action’ compiles, so this must an omission in the dict.

Could somebody please post a full list, possibly copied from a version of the dict that still has them?

Thanks for spotting that!

To judge from the Apple-supplied “Folder Actions” scripts in the “Computer Scripts” folder (see the top submenu in Script Menu), you now have to ‘make’ new folder actions and ‘make’ new scripts at the end of their scripts. It’s good that the syntax has been standardised, but this is the first I’ve heard of it. Apple’s scripts are dated 2007, though! :confused:

Hans, thank you. That now attaches the second script, but sadly the second script does not run when the folder is created by Aperture, probably because Aperture can only do this with an image file included. When just using Finder to add a new folder to current_event, and then using Aperture to add images to that folder, it works a treat.

Thank you once more for your brilliant input. I owe you a beer :slight_smile:

Found them here. But that post is from 2005. Still, they all compile in 10.6:

tell application "System Events"
	attach action to a_item using PathtoFolderaction
	attached scripts a_item -- list actions
	edit action of a_item -- using action name or index
	remove action from a_item -- using action name or index
end tell

I should say that I had to change the second script elements thePic to theImage, it now looks like this,


on adding folder items to this_folder after receiving these_items
	set Photos to (this_folder as text) & "Photos"
	set Thumbs to (this_folder as text) & "Thumbs"
	
	repeat with aFile in these_items
		
		tell application "Image Events"
			activate
			set theImage to open aFile
			scale theImage to size 800
			save theImage as JPEG in file Photos
			
			scale theImage to size 240
			save theImage as JPEG in file Thumbs
			try
				close theImage
			on error
				delay 2
				close theImage
			end try
			
		end tell
	end repeat
	
	
end adding folder items to

Hi Michael,

see if I got you right:
The second script is now attached, but nothing happens … may be it’s because the Pictures are allready in the folder in the moment the folderaction is attached. If this is true, I tried a workaround …


on adding folder items to this_folder after receiving these_items
    
    set PathtoFolderaction to "Mac Red-Grafik 3:Users:claessen:Library:Scripts:Folder Action Scripts:PhotosandThumbs.scpt" --still a samplePath ;-)
    
    repeat with a_item in these_items
        
        if folder of (info for a_item) is true then
            
            try
                tell application "Finder"
                    set Photos to make new folder at a_item with properties {name:"Photos"}
                    set Thumbs to make new folder at a_item with properties {name:"Thumbs"}
                end tell
                
                tell application "System Events"
                    attach action to folder (a_item as text) using PathtoFolderaction
                end tell
                
                set additem to (path to desktop as text) & "addItem.txt"
                do shell script "echo " & quoted form of "Create a file to trigger the folderaction" & " >> " & quoted form of POSIX path of additem
                
                tell application "Finder" to move file (additem as alias) to folder a_item with replacing
                
                
                
                
            end try
        end if
        
    end repeat
end adding folder items to

global Photos
global Thumbs


on adding folder items to this_folder after receiving these_items
    set Photos to (this_folder as text) & "Photos"
    set Thumbs to (this_folder as text) & "Thumbs"
    
    repeat with aFile in these_items
        
        my scalePics(aFile)
    end repeat
    
    --get pics that have been allready in the folder, before the folderaction has been attached
    my getPics(this_folder as text, Photos)
    
end adding folder items to

on getPics(SourceFolder, targetFolder)
    
    set ListSourceFolder to list folder SourceFolder without invisibles
    
    
    set ListTargetFolder to list folder targetFolder without invisibles
    
    repeat with thePicName from 1 to count of ListTargetFolder
        copy thePicName to end of ListSourceFolder
    end repeat
    
    set ListSourceFolder to (sortlist ListSourceFolder with remove duplicates) --satimage osax needed
    
    
    repeat with i from 1 to count of ListSourceFolder
        set lostPic to item i of ListSourceFolder
        if lostPic is "Thumbs" or "Photos" or "additem.txt" then
        else
            set lostPicPath to ((SourceFolder & lostPic) as text) as alias
            my scalePics(lostPicPath)
        end if
    end repeat
    
    
end getPics


on scalePics(PicAlias)
    try
        tell application "Image Events"
            activate
            set thePic to open PicAlias
            scale thePic to size 500
            save thePic as JPEG in file Photos
            
            scale thePic to size 50
            save thePic as JPEG in file Thumbs
            try
                close thePic
            on error
                delay 2
                close thePic
            end try
            
        end tell
    end try
    
end scalePics

P.S. You’re not using Snow Leopard, that’s right?
You’ll need the Osax satimage osax to run the second script; download it here at macscripter …

Sunny Day :slight_smile:

Hans

Hi Hans

I am running Snow Leopard, 10.6.4. Does that make a difference? I will look at your new script a bit later on, though for now I have another script that adds the empty folder for me to the specific location of current_event, then your scripts kick in and all works well. If I could get the same script to create a new Project with the same name in Aperture, that would save the small risk of the two no seeing each other due to a small spelling error.
This is saved as an app on the desktop.

try
	
	set newLoc to "Macintosh HD:users:eventphotography:Sites:michaelmartin:current_event:"
	set {text returned:desc} to display dialog "Type the class name here" default answer "XXXX-XXXX-##" buttons {"OK", "Cancel"} default button "OK"
	
	
	do shell script "/bin/mkdir -p " & quoted form of POSIX path of newLoc & ¬
		quoted form of (desc) & ""
	
on error errmsg number errNum
	if errNum is not -128 then display dialog errmsg
end try

I have now altered the previous script as follows and assigned a keyboard short cut to launch it. This now opens an input box where I type a name and then both the Aperture Project and destination folder under ‘current_event’ are created at the same time. Then Hans’ scripts kick in and when images are imported via Aperture, it all works.

I am a happy man :):cool:

try
	
	set newLoc to "Macintosh HD:users:eventphotography:Sites:michaelmartin:current_event:"
	set {text returned:desc} to display dialog "Type the class name here" default answer "XXXX-XXXX-##" buttons {"OK", "Cancel"} default button "OK"
	
	
	do shell script "/bin/mkdir -p " & quoted form of POSIX path of newLoc & ¬
		quoted form of (desc) & ""
	
on error errmsg number errNum
	if errNum is not -128 then display dialog errmsg
end try

tell application "Aperture"
	tell library 1
		make new project with properties {name:desc}
	end tell
end tell

@Michael: cheers :slight_smile: