Help with Folder actions

Hi guys…could someone help me with getting started on a folder action, and fix one that isn’t working properly after doing a system update?

  1. I would like to create a folder action that would take the contents of the folder and pass them all to an Apple Remote Desktop copy command. If 6 images are added to the folder, copy all 6 images to a select group of computers or computer list.

  2. The old script that I have was created with the help of Stefan, and it is having problems with the repeat for some reason. This action will get the resolution of the images that are in the folder and change the filename to be: resolution-year-filename. When it runs through the list of files that are added to the folder it gets to the last one and then starts back at the beginning again and I have to cancel the script.

As a bonus: Is there a way to set the imgName,imageYear once? That way, If I add 5 files I would only have to fill in the name and year once instead of copying and pasting it 5 times.

on adding folder items to theFolder after receiving theAddedItems
	launch application "Image Events"
	try
		set cur_year to year of (current date) as string
		repeat with thisItem in theAddedItems
			tell application "Image Events"
				set theImage to open thisItem
				set imageDimensions to dimensions of theImage
				close theImage
			end tell
			tell imageDimensions to set dimensionString to (item 1 as string) & "x" & item 2 as string
			set imgName to name of (info for {thisItem})
			set NewName to text returned of (display dialog "Please enter a new file name:" default answer imgName)
			set imageYear to text returned of (display dialog "Use the current year?" default answer cur_year)
			tell application "Finder"
				set fileName to name of thisItem
				set name of contents of thisItem to {dimensionString & "-" & imageYear & "-" & NewName & ".jpg"}
			end tell
		end repeat
	on error theMsg
		-- on error, display error message
		set myPOSIX to (POSIX path of thisItem)
		display dialog ("File " & myPOSIX & " could not be renamed:" & return & return & theMsg) buttons "OK" default button 1
	end try
end adding folder items to

Hi,

the reason of calling the script again and again is renaming a file in the hot folder is treated as added a new file.
Regarding the bonus, you can’t rename multiple files once with the same name in the same folder.

The braces around the new file name probably cause the problem.

Try this (untested), it asks once for the year and multiple times for the file names.
If the file name contains the year string “ indicating it’s renamed yet “ the file will be skipped


on adding folder items to theFolder after receiving theAddedItems
	launch application "Image Events"
	try
		set cur_year to year of (current date) as string
		set imageYear to text returned of (display dialog "Use the current year?" default answer cur_year)
		
		repeat with thisItem in theAddedItems
			tell application "Finder" to set imageName to name of thisItem
			if imageName does not contain imageYear then
				tell application "Image Events"
					set theImage to open thisItem
					set {width, height} to dimensions of theImage
					close theImage
				end tell
				set dimensionString to (width as string) & "x" & (height as string)
				set NewName to text returned of (display dialog "Please enter a new file name:" default answer imageName)
				tell application "Finder"
					set name of contents of thisItem to dimensionString & "-" & imageYear & "-" & NewName & ".jpg"
				end tell
			end if
		end repeat
	on error theMsg
		-- on error, display error message
		set myPOSIX to (POSIX path of thisItem)
		display dialog ("File " & myPOSIX & " could not be renamed:" & return & return & theMsg) buttons "OK" default button 1
	end try
end adding folder items to

When the script runs, it sets the year and asks for the first file name. Each time it repeats it just asks for the file name. After the last repeat has completed, it asks for the year one more time and then it completes (either by hitting ok or cancel).

As for the bonus, since the end result of this action is to save a set of graphics in the corrected naming format (not exact same name as the result), why couldn’t you set the name once in the beginning?

Could the process work like this?

  1. copy items to folder at the same time (blah1.jpg, blah2.jpg, blah3.jpg, etc)
  2. Set the year once using dialog
  3. Set the name to be used (box shows the current name of the first file “blah1.jpg” by default)
  4. change file name in the dialog box to “testing” and hit ok (what about a button to tell the script to re-use the new name?)
  5. file name is changed to 800x600-2015-testing.jpg
  6. next repeat hits (re-use name and year) and it is saved as 1280x768-2015-testing.jpg
  7. next repeat hits (re-use name and year) and it is saved as 1360x768-2015-testing.jpg

The end result of the action produces a different file name, so you should be good to not cause any duplicate file names, but wouldn’t the error catch it anyway?

renaming multiple files with the same name without clash requires that all added images in theAddedItems have different dimensions.

But the main problem is the behavior of folder actions to be retriggered after a file in the hot folder has been renamed,
which causes to ask again for the year

Is moving the files to another folder an option?

Hi Mcusr,

Nice Image or whatever that is. How did you do that?

Later,
kel

I wouldn’t have a problem with moving the files after they are renamed. What if we create a new folder inside the original folder based on the name that you are setting in the dialog box?

you will run into the same problem: creating a folder in the hot folder triggers the folder action.

then how about either a folder in the pictures, or something that I can specify in the script?

create a folder Images in the pictures folder, then try this, it creates a new folder for each entered file name in the Images folder (script is untested)


on adding folder items to theFolder after receiving theAddedItems
	set destinationBaseFolder to ((path to pictures folder as text) & "Images:")
	launch application "Image Events"
	try
		set cur_year to year of (current date) as string
		set imageYear to text returned of (display dialog "Use the current year?" default answer cur_year)
		set NewName to text returned of (display dialog "Please enter a new file name:" default answer imageName)
		repeat with thisItem in theAddedItems
			tell application "Finder" to set imageName to name of thisItem
			tell application "Image Events"
				set theImage to open thisItem
				set {width, height} to dimensions of theImage
				close theImage
			end tell
			set dimensionString to (width as string) & "x" & (height as string)
			set newFileName to dimensionString & "-" & imageYear & "-" & NewName & ".jpg"
			tell application "Finder"
				if not (exists folder NewName of folder destinationBaseFolder) then
					make new folder at folder destinationBaseFolder with properties {name:NewName}
				end if
				if exists file newFileName of folder NewName of folder destinationBaseFolder then
					error "File '" & newFileName & "' exists"
				else
					move thisItem to folder NewName of folder destinationBaseFolder
					set name of file imageName of folder NewName of folder destinationBaseFolder to newFileName
				end if
			end tell
		end repeat
	on error theMsg
		-- on error, display error message
		set myPOSIX to (POSIX path of thisItem)
		display dialog ("File " & myPOSIX & " could not be renamed:" & return & return & theMsg) buttons "OK" default button 1
	end try
end adding folder items to

that doesnt seem to be working. it gets to the year, and then stops processing.

I see, change


 set NewName to text returned of (display dialog "Please enter a new file name:" default answer imageName)

to

set NewName to text returned of (display dialog "Please enter a new file name:" default answer "")

that worked perfectly, thanks a lot Stefan!

Any idea about the Apple Remote Desktop copy command?

Sorry, I don’t have ARD