You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript Code for close the "Web Inspector" Window in Safari

Hi,

 

I have a task to get the screen shot from Safari brower. For that i got the code from some where in forum.

I have an issue while close the "Web Inspector" Window in Safari (see below screenshot).

Please guide me (Much better if you provide the applescript code for close this window)

 

Thanks

Asuvath



Posted on May 10, 2021 7:36 AM

Reply
Question marked as Top-ranking reply

Posted on May 10, 2021 9:38 AM

use scripting additions

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


10 replies

May 11, 2021 5:06 AM in response to VikingOSX

Hi,


I have used your options and its working fine for me, but i am struggling to save the PNG in particular path (i have placed my script for your review).


I got an error in the below line


set value of text field 1 of sheet 1 of window 1 to theName in targetFolder


I am not sure, what is the wrong on this code. Can you please check and help me to out from this?


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

Asuvath

May 11, 2021 10:37 AM in response to asuvathdhaman

There are two key flaws with your script as it stands.


One is that you have two different states for targetFolder.


If it already exists on disk then targetFolder is a string:


  set targetFolder to desktopFolder & "Screenshot_From_Safari:" as text


If it does not exist then you create it:


    set targetFolder to make new folder at folder desktopFolder with properties {name:fldnm}


Now it is a Finder folder object.


The biggest problem is that neither of these are usable in the way you are trying to use it.


You are using UI actions to capture a screenshot of a web element. The problem is that the save dialog ONLY accepts the file name in the text field - you can NOT include a Finder folder object here. Even if you include a text object, that would just become part of the file name.


The only way I can see to do this is in two parts - one part to type in the filename to save as (e.g. "Small.png", "Large.png", etc.), and a separate part to navigate to the folder in which to save.


This is how I achieved it:


First, change the folder name to a property so that it is consistent throughout the code:


property fldnm : "Screenshot_From_Safari"

tell application "Finder"
  ...


then change the UI Actions code to navigate to the folder in question. Replace the broken line:


    set value of text field 1 of sheet 1 of window 1 to theName in targetFolder


with:


		set value of text field 1 of sheet 1 of window 1 to theName
		keystroke "g" using {command down, shift down}
		delay 0.5
		keystroke "~/Desktop/" & fldnm as text
		keystroke return


This will enter the file name, then trigger the directory entry dialog (Command-Shift-G) where you can type the path to save to.


Note: usual caveats apply - UI actions such as these are prone to failure should any underlying component shift (e.g. Apple change the name of the elements, other applications are frontmost, etc.).

Applescript Code for close the "Web Inspector" Window in Safari

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.