Named Screenshot Problems...

Hi…

I’m trying to put together a reliable script to take sized, named screenshots.

As if pressing - shift+command+4 - then having the option to rename the resulting file.

The original purpose for the script was to capture and store a local weather report from a website as a (partial) screenshot.

The following script works pretty well from Script Editor… but when saved as as an application and placed in the dock, it sometimes hangs… requiring quitting the script… and the hang is lengthy if the script isn’t quit. No error is thrown.

I think the problem lies in the initial call to System Events. Once past the System Events tell block, everything works fine. I’ve tried all kinds of variations and workarounds, but none seem reliable.

It may be that I need to tell System Events to tell the frontmost process to… and it would be nice to return to that process as well. Other improvements might be in store if…

But I got frustrated, haven’t done my homework and wouldn’t mind some help.

Thanks much for any and all replies.

Peter B.




tell application "System Events"
	keystroke "$" using (command down & shift down)
end tell

tell application "Finder"
	
	update desktop
	
	repeat until file "Picture 1.png" of the desktop exists
	end repeat
	
	set file_ref to (desktop as text) & "Picture 1.png"
	
	activate
	display dialog ¬
		"Rename...?" & return & return default answer "Screenshot" buttons {"Weather", "Rename"} default button 2
	
	copy the result as list to {text_returned, button_pressed}
	
	if button_pressed is "Rename" then
		try
			set name of file file_ref to text_returned & ".png"
		on error
			display dialog ¬
				"A file with this name already exists in this location..." & return & return & "Please choose another file name." & return & return default answer text_returned buttons {"Cancel", "Rename"} default button 2
			set text_returned to text returned of result
			set name of file file_ref to text_returned & ".png"
		end try
	end if
	
	if button_pressed is "Weather" then
		set name of file file_ref to "Weather.png"
		set move_ref to (desktop as text) & "Weather.png"
		if file move_ref exists then
			move file move_ref to startup disk with replacing
		end if
	end if
	
end tell



Hi Peter

I’m using this segment of a larger script to take a screen shot of a certain part of the screen…
Don’t know if it is of any use in your Quest but your welcome to it…

set screenshot to ((POSIX path of (path to desktop)) & "screenpic.png") as string
do shell script "screencapture " & quoted form of screenshot
set this_file to screenshot
try
	tell application "Image Events"
		launch
		set this_image to open this_file
		set {docWidth, docHeight} to the dimensions of this_image
		crop this_image to dimensions {400, 200}
		save this_image with icon
		close this_image
	end tell
on error error_message
	display dialog error_message
end try

Pidge & Jacques:

I tried each of your suggestions and tried variations of each, but I’m actually finding the following more to my tasre and more reliable… run from Script Editor or as app from the Dock.

I know the code is old fashioned… but it seems to work.

Thanks to both of you.

Peter B.




tell application "System Events"
	keystroke "$" using (command down & shift down)
end tell

tell application "Finder"
	
	update desktop
	
	repeat until file "Picture 1.png" of the desktop exists
	end repeat
	
	set file_ref to (desktop as text) & "Picture 1.png"
	
	activate
	display dialog ¬
		"Rename..." & return & return & "     " & "Or Delete..." & return default answer "Screenshot" buttons {"Delete", "Rename"} default button 2 with title "Rename Picture"
	
	copy the result as list to {text_returned, button_pressed}
	
	if button_pressed is "Delete" then
		delete file file_ref
	end if
	
	if button_pressed is "Rename" then
		try
			set name of file file_ref to text_returned & ".png"
		on error
			display dialog ¬
				"A file with this name already exists in this location..." & return & return & "Please choose another file name." & return & return default answer text_returned buttons {"Rename"} default button 1 with title "Rename File"
			
			set text_returned to text returned of result
			try
				set name of file file_ref to text_returned & ".png"
			on error
				set name of file file_ref to "???" & ".png"
				return
				error -128
			end try
		end try
	end if
	
end tell


Warning: Incoming pedantry. :slight_smile:

Although this usually works, it’s one of the basic tenets of AppleScript records that their properties aren’t in any particular order. You’re assuming here that coercing display dialog’s result record to list will always return the values in the same order. It would be better to use the equivalent method for setting variables by record:

display dialog ¬
	"Rename...?" & return & return default answer "Screenshot" buttons {"Weather", "Rename"} default button 2
  
set {text returned:text_returned, button returned:button_pressed} to the result

Actually, although Peter’s syntax is a little strange, the concatenation causes a coercion to list which produces the right result:

tell application "System Events"
	(command down & shift down)
end tell
--> {command down, shift down}

Doing the concatenation inside a list produces a list within a list, which sadly doesn’t work with the keystroke command. :frowning:

What I like about this is that it doesn’t return until the screen capture’s completed, so there’s no need to script a wait for the file to appear on the desktop. (If it appears, of course. The process might be aborted by the user.) Also, since the path to the file has to be specified in advance, it allows for the file to be saved directly with the required name instead of being renamed and moved.

tell application (path to frontmost application as Unicode text)
	display dialog ¬
		"Please chose a name for the screen capture file" & return & return default answer "Screenshot" buttons {"Weather", "Rename"} default button 2
	
	set {text returned:text_returned, button returned:button_pressed} to the result
	
	if (button_pressed is "Rename") then
		repeat
			try
				set capture_path to (path to desktop as Unicode text) & text_returned & ".png"
				-- If this doesn't error, the file already exists.
				capture_path as alias
				display dialog ¬
					"A file with this name already exists in this location..." & return & return & "Please choose another file name." & return & return default answer text_returned buttons {"Cancel", "Rename"} default button 2
				set text_returned to text returned of result
			on error number -43
				exit repeat
			end try
		end repeat
	else
		set capture_path to (path to startup disk as Unicode text) & "Weather.png"
	end if
end tell

do shell script "screencapture -i " & quoted form of (POSIX path of capture_path)

Strangely enough, all three of my, Jacques’, and Nigel’s scripts fail occasionally (as dock applications) in the same fashion (on my machine) for reasons unknown.

When it happens, whatever form of screen capture command is used (at beginning or end of script), dragging the cursor produces no selection and no resulting file. The cursor returns to the usual arrow, the script continues running until quit OR until the command key is pressed… when the target cursor reappears and the script runs as intended. Additional strangeness.

That’s actually the problem that prompted my post… but I thought it was an anomaly of my code alone. I spent the better part of a day headbanging, trying to figure it out.

I guess I can live with it since I know how to handle it when it happens, but it remains a curiosity.

Now then… here’s a little snippet to distinguish between pedantry and didacticism. Uncomment the indicated line to refine your search.

Thanks much for the alternate examples.

Peter B.




set look_up to the clipboard

tell application "Dictionary" to activate

tell application "System Events"
	keystroke "a" using command down
	keystroke (delete)
	keystroke look_up
	--keystroke (ASCII character 3)
end tell