Automated screen grabs... help required please...

Hi there,

Over the past couple of days I’ve been putting together an automated screen grab script.

The first version of the script works fine, I can drag and drop several files onto the script and the screen grabs are placed in a folder called ‘screen_grabs’ on my desktop.
I was wanting to take the script one step further so the screen grabs are saved in the same folder as the individual files dropped onto it and therefore save me time having to do the file management.

Here’s the script so far:-

on open (someItems)
	
	repeat with theitem in someItems
		tell application "Finder" to set thePath to (container of (theitem)) as string
		
		set thePathPosix to POSIX path of thePath
		
		set {atid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
		set fileName to the last text item of (theitem as text)
		set AppleScript's text item delimiters to atid
		
		(*
			TELL APPLICATION - OPEN theItem
		*)
		
		set format_ to "png"
		
		do shell script "/usr/sbin/screencapture -mt" & format_ & thePathPosix & fileName & "." & format_
		
		(*
			TELL APPLICATION - CLOSE theItem
		*)
		
	end repeat
	
end open

As mentioned, version 1 of the script works fine however version 2 doesn’t. I’m pretty sure it’s to with the way I’m combining the ‘thePathPosix’ with the rest of this:-

do shell script "/usr/sbin/screencapture -mt" & format_ & thePathPosix & fileName & "." & format_

I think the wrong path is being generated therefore the above line fails.

Please can someone point me in the right direction.

Thanks,

Nick

Hi Nick,

there are some space characters missing in the shell script line to separate the parameters
Try this


on open someItems
	repeat with theitem in someItems
		tell application "Finder" to set {fileName, thePath} to {name of theitem, container of theitem as text}
		set thePathPosix to POSIX path of thePath
		(*
			TELL APPLICATION - OPEN theItem
		*)
		
		set format_ to "png"
		do shell script "/usr/sbin/screencapture -m -t " & format_ & space & quoted form of (thePathPosix & fileName & "." & format_)
		
		(*
			TELL APPLICATION - CLOSE theItem
		*)
		
	end repeat
end open

Hi Stefan,

Thanks for your help my query and for tidying up my code. Once again it’s been an education, it’s great being able to compare your tweaks with my original.

I’ve tried implementing your changes however the script still appears to be failing when it comes to the saving of the screen grab. I’ve tried the obvious like filename too long and getting rid of odd characters but still no joy?
The files are opening up and closing ok.

Please can you suggest something else I can try.

Thanks again for your help Stefan.

Regards,

Nick

Hi Stefan,

Have managed to get this working now. :slight_smile:

I made the change below and the script works perfectly.

do shell script "/usr/sbin/screencapture -m -t " & format_ &

for

do shell script "/usr/sbin/screencapture -mt" & format_ &

I changed ’ -m -t ’ for this ’ -mt’ and it seems ok.
Can you tell me if there’s anything wrong with the change.

Thanks again Stefan.

Best Regards,

Nick

That’s the proper syntax of my script :wink:
Anyway there must be a space character after the t flag.
A flag like the t flag with a following parameter must be specified separately
the shell line without variables must look like


do shell script "/usr/sbin/screencapture -m -t png '/path/to/image.png'"

Hi Stefan,

Thanks again for your help with this and the explanation.
It made things a little clearer. The script is working great.

Thanks again,

Nick