AppleScript shows error while save the PNG in path

Hi,

I would like to save the screen shot from Safari Browser, in the path “~Desktop/Screenshot_From_Safari”, for that i have developed the below codes, but unfortunately it shows the error while save the path in the below line

    set value of text field 1 of sheet 1 of window 1 to theName in [b]targetFolder[/b]

Can any one check and help me to resolve this error? and how do i save the screen shot in thiis path.

tell application "Finder"
	set desktopFolder to (path to desktop)
	set fldnm to "Screenshot_From_Safari"
	set targetFolder to desktopFolder & "Screenshot_From_Safari:" as text
	set rslt to my FldrExists(targetFolder)
	if rslt is false then
		set targetFolder to make new folder at folder desktopFolder with properties {name:fldnm}
	else
		delete (every item of folder (targetFolder) whose name ends with ".png")
	end if
	
	tell application "Safari"
		activate
		open location "https://www.apple.com"
		set bounds of front window to {0, 0, 900, 900}
		my screenCapture("Small.png", targetFolder)
		delay 0.1
		my webIns()
		set bounds of front window to {0, 0, 1200, 900}
		my screenCapture("Medium.png", targetFolder)
		delay 0.1
		my webIns()
		set bounds of front window to {0, 0, 1400, 900}
		my screenCapture("Large.png", targetFolder)
		delay 0.1
	end tell
end tell
tell application "Safari" to close tab 1 of window 1

on screenCapture(theName as text, targetFolder)
	tell application "System Events" to tell application process "Safari"
		repeat until (UI element "Reload this page" of group 2 of toolbar 1 of window 1 exists)
			delay 0.1
		end repeat
		--display notification "The webpage is fully loaded now." sound name "Frog"
		click menu item "Show Web Inspector" of menu 1 of menu bar item "Develop" of menu bar 1
		delay 1
		set theSelection to value of attribute "AXFocusedUIElement"
		tell theSelection to perform action "AXShowMenu"
		delay 1
		keystroke "Capture Screenshot" & return
		
		repeat until sheet 1 of window 1 exists
			delay 0.01
		end repeat
		
		set value of text field 1 of sheet 1 of window 1 to theName in targetFolder
		click button "Save" of sheet 1 of window 1
		--display alert sheet 1
	end tell
end screenCapture

on webIns()
	tell application "System Events"
		activate
		tell application process "Safari"
			set frontmost to true
			# toggle Safari web inspector panel on or off
			keystroke "i" using {option down, command down}
		end tell
	end tell
end webIns

on FldrExists(theFldr) -- (String) as Boolean
	tell application "System Events"
		if exists folder theFldr then
			return true
		else
			return false
		end if
	end tell
end FldrExists

on FileExists(theFle) -- (String) as Boolean
	tell application "System Events"
		if exists file theFle then
			return true
		else
			return false
		end if
	end tell
end FileExists

Thanks in Advance
Asuvath

Model: iMac
AppleScript: Applscript 2.7
Browser: Safari 537.36
Operating System: macOS 10.14

I’m not especially knowledgeable about ui scripting but I suspect that your issue is that you need to set Safari’s save folder (e.g. targetFolder) using ui scripting. I don’t know how else you can set the value of sheet 1’s ‘Where:’ popup.

So try something like this:


	set value of text field 1 of sheet 1 of window 1 to theName
	keystroke "g" using {shift down, command down}
	keystroke "a" using command down
	key code 51 -- delete (prior string)
	delay 0.2
	keystroke "~/Desktop/Screenshot_From_Safari"
	delay 0.2
	key code 36 -- return
	delay 0.2
	key code 36 -- return

Essentially, it uses the command-shift-g shortcut to ‘go’ to targetFolder.

Alternatively, if you set the posix path of targetFolder somewhere, e.g.

set tf to posix path of targetFolder"

… then you can use that variable to supply the keystroke, e.g.

-- keystroke "~/Desktop/Screenshot_From_Safari"
keystroke tf

To do this, you may need to make tf global. Dunno if it only a posix path can be accepted, also dunno if all those delays are required, but….