AppleScript Code for rename while save in particular path

Hi,

My script take the screen shot from browser, but i am struggling to fix the name that screenshot in script in required path while save that.

I want to rename that screen shot as “Small.png” in path (Desktop/Screenshot_From_Safari).

Can anyone look into this and help me to out from this?

Thanks
Asuvath

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

Asuvathdhaman. You don’t explain how you are going to get the path to the screenshot file that will be moved and renamed. I’ve just specified that path in the script but this will need to be changed to fit your needs. If you are going to move and rename a large number of screenshot files at one time, a different approach should probably be used, but otherwise the Finder will do the job well.

set sourceFolder to (path to home folder as text) & "Screenshots:"
set sourceFile to sourceFolder & "My Screenshot.png"
set targetFolder to (path to desktop as text) & "Screenshot_From_Safari:"

tell application "Finder"
	set theFile to (move file sourceFile to folder targetFolder)
	set the name of theFile to "Small.png"
end tell

If you want to copy rather than move the screenshot file, substitute duplicate for move in the above script.

Hi peavine,

Thanks for the response. Actually my script is for “Safari” Browser. I have placed my code here.

This script will provide 3 screen shots, i will rename this screenshot as “Small.png”, “Medium.png” and “Large.png” in ~Desktop/Safari/PNG.

I am new for applescript, i have collect code from some of the forum and created as new script, its is working for me, but i cannot able to rename and save those in particular path.

tell application "Safari"
	activate
	open location "https://www.apple.com"
	set bounds of front window to {0, 0, 900, 900}
	my screenCapture()
	delay 0.1
	set bounds of front window to {0, 0, 1200, 900}
	my screenCapture()
	delay 0.1
	set bounds of front window to {0, 0, 1400, 900}
	my screenCapture()
	delay 0.1
end tell
tell application "Safari" to close tab 1 of window 1

on screenCapture()
	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
		
		click button "Save" of sheet 1 of window 1
		--tell application "Safari" to close the tabpanel of window 1
		--display alert sheet 1
	end tell
end screenCapture

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

Asuvathdhaman. My knowledge of GUI scripting is extremely limited and I am unable to assist with your script. I’m sure another forum member will be able to help. BTW, I wasn’t aware you could take a screenshot in that fashion–that’s interesting.

The “screenshot” taken by the OP’s script captures the entire apple.com web page not just the portion shown on the screen. Thus, the initial screenshot taken with the OP’s script on my computer is 3,022 pixels wide by 8,808 pixels high. So, there does appear to be a difference.

Hi Asuvathdhaman. Welcome to MacScripter.

Just to point out that MacScripter has its own posting tags for AppleScript code, namely [applescript] and [/applescript]. If you wrap your code in these when posting it, it’ll appear in a box with a clickable link to open it in people’s default script editors. (I’ve done this for you in post #3 above.) There’s a button for these tags just above the text field when you post.

Hope you get a solution to your query. :slight_smile:

Asuvathdhaman. If a forum member doesn’t provide a GUI scripting solution, you may want to let the script create the screenshot files with their default locations and names and then to move and rename them afterwards. For example:

set targetFolder to (path to desktop as text) & "Screenshot_From_Safari:"
set fileNames to {"big.png", "medium.png", "small.png"}

tell application "Finder"
	set ssFiles to every file in (path to desktop) whose name begins with "Screen Shot"
	set ssFiles to sort ssFiles by modification date
	if (count ssFiles) ≠ (count fileNames) then display dialog "The desktop does not contain the required screenshot files" buttons "OK" cancel button 1 default button 1
	repeat with i from 1 to (count ssFiles)
		set theFile to (move (item i of ssFiles) to folder targetFolder)
		set the name of theFile to item i of fileNames
	end repeat
end tell

BTW, this script throws an error if the screenshot files exist at the destination folder. I didn’t know how you would want to resolve this.

This is altered version of your code will not throw an error if the file already exists. It will simply add a number to the new name like this “Small 2.png” if there is only 1 already existing file. If there are two existing files then the new file name would be “Small 3.png"… and so on.

set desktopFolder to (path to desktop)
set targetFolder to (path to desktop as text) & "Screenshot_From_Safari:"
set fileNames to {"Large.png", "Medium.png", "Small.png"}

tell application "Finder"
	set ssFiles to every file in desktopFolder whose name begins with "Screen Shot"
	set ssFiles to sort ssFiles by modification date
	if (count ssFiles) ≠ (count fileNames) then display dialog ¬
		"The desktop does not contain the required screenshot files" buttons "OK" cancel button 1 default button 1
	repeat with i from 1 to (count ssFiles)
		tell current application to set theName to text 1 thru ¬
			((offset of ".png" in (item i of fileNames)) - 1) of item i of fileNames
		set theCount to count (files of alias targetFolder whose name contains theName)
		set theFile to (move (item i of ssFiles) to folder targetFolder)
		if theCount = 0 then
			set the name of theFile to item i of fileNames
		else
			set the name of theFile to theName & " " & ((theCount + 1) as text) & ".png"
		end if
	end repeat
end tell

I think, this “forum member” is me, because the capture GUI script above is written by me, one year early. Well, the OP can set text field 1 of sheet 1 of window 1 of process “Safari” to the desired name. Here is fully tested script for the latest Safari (Version 14.1 (15611.1.21.161.7, 15611)):


tell application "Safari"
	activate
	open location "https://www.apple.com"
	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
		keystroke "i" using {command down, option down} -- Show Web Inspector
	end tell
end tell
display notification "The webpage is fully loaded now." sound name "Frog"

set bounds of front window to {0, 0, 900, 900}
my screenCapture("Small.png")
set bounds of front window to {0, 0, 1200, 900}
my screenCapture("Medium.png")
set bounds of front window to {0, 0, 1400, 900}
my screenCapture("Large.png")

tell application "Safari" to close tab 1 of window 1

on screenCapture(theName as text)
	tell application "System Events" to tell application process "Safari" to tell window 1
		repeat until row 2 of outline 1 of group 4 of UI element 1 of scroll area 1 of group 1 of group 1 of tab group 1 of splitter group 1 exists
			delay 0.1
		end repeat
		tell row 2 of outline 1 of group 4 of UI element 1 of scroll area 1 of group 1 of group 1 of tab group 1 of splitter group 1 to perform action "AXShowMenu"
		repeat until menu item "Capture Screenshot" of menu 1 of group 1 of tab group 1 of splitter group 1 exists
			delay 0.1
		end repeat
		click menu item "Capture Screenshot" of menu 1 of group 1 of tab group 1 of splitter group 1
		repeat until sheet 1 exists
			delay 0.01
		end repeat
		set value of text field 1 of sheet 1 to theName
		click button "Save" of sheet 1
		repeat while sheet 1 exists
			delay 0.1
		end repeat
	end tell
end screenCapture

Hi All,

All of your suggestions are very helpful to achieve my task. Thank you for wonderful support for get the solution.

I got all the solution except save the PNG in respective path.

My task on this is, need to save the PNG in “~Desktop/Screenshot_From_Safari” folder.

Condition 1: If the folder is not exists then, we need create the folder “Screenshot_From_Safari”, else if the older already exists then, we need to place the png file into that.

Condition 2: If the folder already holding the PNG (Small.png, Medium.png, and Large.png) then we need to replace existing ones.

Thanks in Advance
Asuvath

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 this 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

Here is updated script for Safari Version 14.1 (15611.1.21.161.7, 15611). It does all what you asked for.


property desktopHFS : (path to desktop folder) as text
property destinationFoldeHFS : desktopHFS & "Screenshot_From_Safari"
property destinationFolderPosixPath : POSIX path of destinationFoldeHFS

-- Make destination folder if not exists
try
	alias destinationFoldeHFS
on error
	tell application "System Events" to ¬
		make new folder at folder desktopHFS with properties {name:"Screenshot_From_Safari"}
end try

-- Load webpage, Show Web Inspector
tell application "Safari"
	activate
	open location "https://www.apple.com"
	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
		keystroke "i" using {command down, option down} -- Show Web Inspector
	end tell
end tell
display notification "The webpage is fully loaded now." sound name "Frog"

-- Save screenshots, close tab
set bounds of front window to {0, 0, 900, 900}
my screenCapture("Small.png")
set bounds of front window to {0, 0, 1200, 900}
my screenCapture("Medium.png")
set bounds of front window to {0, 0, 1400, 900}
my screenCapture("Large.png")
tell application "Safari" to close tab 1 of window 1

on screenCapture(theName as text)
	tell application "System Events" to tell application process "Safari" to tell window 1
		
		-- Capture Screenshot
		repeat until row 2 of outline 1 of group 4 of UI element 1 of scroll area 1 of group 1 of group 1 of tab group 1 of splitter group 1 exists
			delay 0.1
		end repeat
		tell row 2 of outline 1 of group 4 of UI element 1 of scroll area 1 of group 1 of group 1 of tab group 1 of splitter group 1 to perform action "AXShowMenu"
		repeat until menu item "Capture Screenshot" of menu 1 of group 1 of tab group 1 of splitter group 1 exists
			delay 0.1
		end repeat
		click menu item "Capture Screenshot" of menu 1 of group 1 of tab group 1 of splitter group 1
		repeat until sheet 1 exists
			delay 0.01
		end repeat
		
		-- Set (go to) destination folder
		keystroke "g" using {command down, shift down}
		repeat until combo box 1 of sheet 1 of sheet 1 exists
			delay 0.1
		end repeat
		set value of combo box 1 of sheet 1 of sheet 1 to destinationFolderPosixPath
		click button 1 of sheet 1 of sheet 1 --to accept folder
		
		-- Save screenshot with replacing
		repeat until text field 1 of sheet 1 exists
			delay 0.1
		end repeat
		set value of text field 1 of sheet 1 to theName
		click button "Save" of sheet 1
		if exists UI element "Replace" of sheet 1 of sheet 1 then click UI element "Replace" of sheet 1 of sheet 1
		repeat while sheet 1 exists
			delay 0.1
		end repeat
		
	end tell
end screenCapture