Find the most recent file in folder?

Hey,

Can this be modified to set theImage to the most recent file added as opposed to it’s name?

set theImage to last file of folder EyeFiToProcessFolder whose name starts with "D"

Thanks,

Carl

tell application "Finder"
	set theFolder to "/Users/ckeyes888/EyeFiToProcessFolder"
	set xxx to first item of (every paragraph of (do shell script "ls -t " & theFolder))
end tell

Thanks a bunch. Shouldn’t this work? Can’t get it going.

set EyeFiToProcessFolder to (path to desktop as text) & "EyeFi to Process:"
		
		set theOutputFolder to "/Users/TV/Sites/1.jpg"
		
		tell application "Finder"
			
			set theImage to first item of (every paragraph of (do shell script "ls -t " & EyeFiToProcessFolder))
			
		end tell

Carl

Actually, might be better to show you the whole script.

on adding folder items to this_folder after receiving added_items
	try
		
		set EyeFiToProcessFolder to (path to desktop as text) & "EyeFi to Process:"
		---set theOutputFolder to (path to desktop as text) & "EyeFi Images:1.jpg"
		set theOutputFolder to "/Users/TV/Sites/1.jpg"
		
		tell application "Finder"
	
			set theImage to first item of (every paragraph of (do shell script "ls -t " & EyeFiToProcessFolder))
			
		end tell
		
		tell application "Image Events"
			launch
			set theImageReference to open file (theImage as text)
			tell theImageReference
				scale it to size 700
				save in theOutputFolder as JPEG
				close
				
				tell application "IndigoServer"
					execute group "EyeFi on Display Screen"
					
				end tell
			end tell
		end tell
		set theFolder to (path to desktop as text) & "EyeFi to Process:"
		set numberOfFiles to count of (paragraphs of (do shell script "mdfind -onlyin " & quoted form of POSIX path of theFolder & " kMDItemFSInvisible == 0"))
		tell application "IndigoServer"
			set the value of variable "EyeFi_Count" to numberOfFiles
		end tell
		
	end try
end adding folder items to

Someday I’ll figure out how to add applescript correctly here.

With your script selected, click on the AppleScript button above the entry pane, or click on the button and paste your script between the BBCodes. BBCodes have to be stopped, so the second one always begins with a slash.
Adam

Thanks,

Carl

Hi, Carl (and John).

  1. Your Folder Actions script makes no reference to the items which trigger it or to the folder to which it’s attached.
  2. The value of ‘EyeFiToProcessFolder’ is an HFS path (the kind with colons), whereas shell scripts require POSIX paths (the type with slashes).
  3. Confusingly, the value of ‘theOutputFolder’ is a POSIX path to what looks like a JPEG file. Looking ahead to the Image Events part of the script, it’s probably the path which is right (if you have a user called “TV”) and the variable label which is misleading.
  4. You don’t need to (and shouldn’t) tell the Finder to execute a shell script.
  5. It’s more efficient to get the ‘first paragraph’ of a text (if that’s all you want) than to get the ‘first item of every paragraph’.
  6. The value of ‘theImage’ is (or will be when the above’s sorted out) just the name of the the most recently modified item in the folder. You’ll need a full path in HFS format for the Image Events part of the code. It’ll already be text, so there’s no need for the ‘as text’ in the Image Events ‘open’ command.
  7. I’m not familiar with IndigoServer, but generally, you should avoid nesting ‘tell’ statements aimed at different applications.
  8. ‘theFolder’ has exactly the same value as ‘EyeFiToProcessFolder’, so you could reuse the latter instead of making another call to the ‘path to’ function. Here you’ve correctly converted the HFS path to a quoted POSIX path for the “mdfind” shell script.
  9. ‘count’ is a command, not a property, so putting ‘of’ after it is technically wrong, although in practice the ‘of’ is ignored and it makes no difference.

Hope this helps. I haven’t posted a corrected script because I don’t understand what’s supposed to be happening with the folder action and, as I’ve said, I’m not familiar with IndigoServer.

Carl, to find the name of the latest file in a folder, you can use this:
(Nigel’s corrections are incorporated)


set theFolder to "/Users/ckeyes888/EyeFiToProcessFolder"
set xxx to first paragraph of (do shell script "ls -t " & theFolder)

Also, why don’t you explain from beginning to end what you are trying to accomplish with your script, listing each step.

The process I’m after is when I take a photo my home automation software, Indigo, displays it on a screen.

The camera has an Eye Fi card which can send images from the camera to any folder on your computer.
The script is setup as a folder action to scale, rename and save the incoming image to the folder where Indigo can
use it for display. It also tells Indigo the number of files in the folder that Eye Fi uses to store the images.

All seems to be working pretty well. I know the script is a hodgepodge but considering how little I know
about AppleScript I’m surprised it works at all.

Thanks,

Carl

You can attach this folder action to the EyeFi to Process folder. I can’t test the IndigoServer portion of the script.

property theOutputFolder : "/Users/TV/Sites/"

on adding folder items to this_folder after receiving added_items
	repeat with anItem in added_items
		
		tell application "Image Events"
			activate
			set theImageReference to open anItem
			tell theImageReference
				scale it to size 700
				save in theOutputFolder as JPEG
				close
			end tell
		end tell
		
		(*
			   	tell application "IndigoServer"
			   	 execute group "EyeFi on Display Screen"
			   	end tell
			   *)
		
		set numberOfFiles to count of (list folder ((path to desktop) & "EyeFi to Process" as text) without invisibles)
		
		(*
	   	tell application "IndigoServer"
	   	 set the value of variable "EyeFi_Count" to numberOfFiles
	   	end tell
	   *)
		
	end repeat
end adding folder items to