Screenshot Script - Help Appreciated!

Always use quoted form to consider space characters


do shell script "echo " & quoted form of posix_path

I might be wrong. Just bored with nothing to do.

Hi Stefan,

That’s what I was thinking about writing, but was too lazy. Spaces would cause errors. Thanks for clarifying that.

kel

You two are AMAZING.
I’m having the issue below though:

"Google Chrome got an error. ImageIO could not open ‘/.Folder:First Folder:Second Folder:130713 Google HOmepage.jpg-GXdj’
error = 13
Create destination parameter is nil
screencapture: cannot write file to intended destination ‘/.Folder:First Folder:Second Folder:130713 Google HOmepage.jpg’

	--Set path and create directory structure--
	tell application "Finder"
		set screenPath to alias "Folder:First Folder:Second Folder:"
		set picPath to (screenPath & "Screenshots:") as string
		if not (exists folder picPath) then
			do shell script "mkdir -p " & quoted form of POSIX path of picPath
		end if
		
	end tell
	--end set path and directory structure-->
	
	--Open URL in Chrome--
	tell application "Google Chrome"
		activate
		tell active tab of window 1
			set URL to currentUrl
		end tell
		--end open in Chrome--
		
		--Delay, then screenshot--		
		delay 5
		set picName to (dateStamp & " " & siteName & " " & pageName & ".jpg") as string
		do shell script "screencapture -tjpg " & quoted form of (picPath & picName)
	end tell
	--end delay and screenshot--

(just a snippet of the whole script above, if you need the rest - just say!)

Hmm, just tried to make a donation to the forum as a little thanks for all the help. “Return to merchant and try a different payment method” - I’ll try again later!

there must be a space character between -t and jpg

I don’t think that’s what is causing my error - It has been -tjpg the entire time and been working fine.
It is just when I’ve tried to make it save to the fileserver address rather than local that I run into issues.

The “Set path and create directory file structure” part works, writing to server fine - it is just telling the screencapture bit to write to the server in the correct format, I must have something a bit wrong in my format etc?

(Would test out the -tjpg thing now, but not at my laptop again for another few hours).

I doubt that you get a proper jpg file with this syntax.
In a shell script a parameter token and its argument(s) must be always separated by a space character

thanks for that - I will remedy.

Any thoughts on actually getting the screenshots saving at all still welcome :slight_smile:

I recommend to put the shell script part out of the chrome tell block
and the path for screen capture must be a POSIX path too


tell application "Google Chrome"
     activate
     tell active tab of window 1
         set URL to currentUrl
     end tell
     --end open in Chrome--
end tell

--Delay, then screenshot--        
delay 5
set picName to dateStamp & " " & siteName & " " & pageName & ".jpg
do shell script "screencapture -t jpg " & quoted form of (POSIX path of picPath & picName)

What a controversial way screencapture parses arguments!

Like Stefan, I haven’t seen that syntax for commandline parameters too often. But come to think about it, I may have, after all, without any concrete recollections at the moment.

Anyway, I tested the commandline without the superfluos space betweeen parameter and option, and it worked for me! :smiley:

Edit

I think it is a feature (built-in) of the getopt(3) or getopt_long(3) library (in libstdc), I think at least one of them (probably getopt_long) also accepts a “:” between the option and the argument, as a replacement for the space, or the void.

Longopts can have arguments interspersed between the options, and this probably makes the parsing easier in these cases, so that you can differ between arguments for options, and the real arguments that follows, when the options are processed.

By the way, I have revised the trim handler used above, to become somewhat more effective, commented, and terse.

on trim(R)
	# R { stringToStrip:_str, charlistToRemoveAsNumbers:_char}
	# set _whites to {32, 9, 13, 10}
	# space, tab, carriage return, line feed
	local _strBuf
	set _strBuf to (stringToStrip of R) as string
	tell (charlistToRemoveAsNumbers of R)
		# Scan for and trim all unwanted characters from the beginning of the line
		if it contains (id of (text 1 of _strBuf)) then
			repeat until it does not contain (id of (text 1 of _strBuf))
				try
					set _strBuf to (text 2 thru -1 of _strBuf)
				on error
					# its empty
					set _strBuf to ""
					return _strBuf
				end try
			end repeat
		end if
		if it contains (id of (text -1 of _strBuf)) then
			# Scan for and trim all unwanted characters from the end of the line
			# it won't be empty here, since a line only consisting of characters
			# that are to be trimmed are covered when we trim the start of the line.
			repeat until not (it contains (id of (text -1 of _strBuf)))
				set _strBuf to (text 1 thru -2 of _strBuf)
			end repeat
		end if
	end tell
	return _strBuf
end trim

Thanks for all the help! The script is in near perfect working order.
My only issue is that when this is saved as an Application - whenever it is opened it comes up with “<> doesn’t understand the dateStamp message.”

Any clues?

--Generate datestamp--
on dateStamp()
	tell (current date) to return text 3 thru 8 of (year * 10000 + (its month as integer) * 100 + day as string)
end dateStamp
set dateStamp to dateStamp()
--end datestamp-- 

--Set Variables--
property whichUrl : 1
property siteID : 2
property nameUrl : 3
property delayCount : 4
--end variables--

--Open site database--
set theFile to alias "[PATH HIDDEN]"
tell application "TextEdit"
	activate
	open theFile
end tell
--end open-- 

--Open Chrome and focus--
tell application "Google Chrome"
	open
	activate
	make new window
end tell
--end open Chrome and focus--

--Fullscreen Chrome--
tell application "System Events"
	keystroke "f" using {command down, shift down}
end tell
--end fullscreen-

--Get number of sites and repeat for that number--
tell application "TextEdit"
	set theCount to the count of paragraphs of front document
end tell
repeat theCount / 4 times
	--end number of sites--
	
	
	--Set instance specific variables--
	tell application "TextEdit" to set txt to text of its front document
	
	set currentUrl to paragraph whichUrl of txt
	set siteName to paragraph siteID of txt
	set pageName to paragraph nameUrl of txt
	set delayTime to paragraph delayCount of txt
	--end instance specific variables--
	
	--Set path and create directory structure-- 
	tell application "Finder"
		set screenPath to alias "[PATH HIDDEN]"
		set picPath to (screenPath & siteName & ":" & pageName & ":") as string
		if not (exists folder picPath) then
			do shell script "mkdir -p " & quoted form of POSIX path of picPath
		end if
		
	end tell
	--end set path and directory structure-->
	
	--Open URL in Chrome--
	tell application "Google Chrome"
		activate
		tell active tab of window 1
			set URL to currentUrl
		end tell
		--end open in Chrome--
		
		--Delay, then screenshot--		
		delay delayTime
		set picName to (dateStamp & " " & siteName & " " & pageName & ".jpg") as string
		do shell script "screencapture -t jpg " & quoted form of (POSIX path of picPath & picName)
	end tell
	--end delay and screenshot--
	
	--Set variables for next run--
	set whichUrl to whichUrl + 4
	set siteID to siteID + 4
	set nameUrl to nameUrl + 4
	set delayCount to delayCount + 4
	--end set variables for next run--
end repeat

--Close Chrome--
tell application "System Events"
	keystroke "w" using {command down}
end tell
--end close--

--Open screenshot folder--
tell application "Finder"
	open folder screenPath
	activate
end tell
--end open screenshot folder--

EDIT: This also happens a lot from the AppleScript Editor too! I solve the problem by adding and deleting a space somewhere in the doc (i.e. “space” and then “backspace” - no NET change) - odd?

Hi.

Your dateStamp handler has the same label (‘dateStamp’) as the variable you’re using to store the result. This of course changes the value of dateStamp from the handler to the result the first time you run the script. Since variables at the top level of a script are persistent, the script tries to execute the date stamp on subsequent runs instead of the handler!

Just change the name of either the handler or the storage variable.

That worked perfectly!

Thanks everybody, I’ve got a perfectly functioning script now. Too cool!