Keep safari remote debugging open on navigation - safari

I'm using Safari's remote debugging to inspect a webview in an iPhone app in my simulator. The problem is that the remote debugging window closes as soon as the app does.
I have an action which switches to another app and back but I can't read console.log messages from immediately before the switch because I'm not quick enough and I can't read logs from immediately after coming back to my app because I have to re-open the console first.
Is there a way to keep it open so I can at least see the last logs from before switching apps?

Here is an AppleScript that launches Safari Inspector. You can export it as an executable application and have it sitting in your dock to get into Inspector with a single click or launch it in a build phase in Xcode.
tell application "Safari"
activate
delay 2
tell application "System Events"
tell process "Safari"
set frontmost to true
click menu item 2 of menu 1 of menu item "iPad Simulator" of menu 1 of menu bar item "Develop" of menu bar 1
end tell
end tell
end tell

This is James' answer wrapped in an Alfred Workflow, -g- ... check it
ALSO WORKS FOR IPHONE BRAH
on alfred_script(q)
tell application "Safari"
activate
delay 0.5
tell application "System Events"
tell process "Safari"
set frontmost to true
try
click menu item 2 of menu 1 of menu item "iPhone Simulator" of menu 1 of menu bar item "Develop" of menu bar 1
end try
try
click menu item 2 of menu 1 of menu item "iPad Simulator" of menu 1 of menu bar item "Develop" of menu bar 1
end try
end tell
end tell
end tell
end alfred_script

Related

Applescript access Safari's "Reload this page" button update

Classic problem: Wait until the webpage has fully loaded and rendered in Safari.
Most snippets I found date years back and of course macOS and Safari have been upgraded, so I am looking how to TARGET this Reload this page button
I am on macOS Big Sur 11.7.2 with Safari v16.2, attached a screenshot (Note the beta Translate button)
This snipped should have been working on macOS High Sierra
tell application "Safari" to make new document with properties {URL:"https://nytimes.com"}
tell application "System Events"
repeat until (accessibility description of ¬
button 1 of UI element 2 of every group of toolbar 1 of window 1 of ¬
process "Safari" whose name = "Reload this page") contains "Reload this page"
delay 0.5
end repeat
end tell
log "Finished loading"
I tried to change button 1 of UI element 2 to button 2 assuming button 1 could be the Translate button but no go.
Ahhh, I just found it, with a better Google search, on
https://apple.stackexchange.com/questions/424629/applescript-to-prompt-for-url-input-open-url-in-safari-then-open-bookmark-with
So just for a reference:
tell application "Safari" to make new document with properties {URL:"https://nytimes.com"}
my waitForSafariPageToFinishLoading()
to waitForSafariPageToFinishLoading()
-- # Wait for page to finish loading in Safari.
-- # This works in **macOS Catalina** and
-- # macOS Big Sur and may need adjusting for
-- # other versions of macOS.
log "page is loading...."
tell application "System Events" to repeat until ¬
exists (buttons of groups of toolbar 1 of window 1 of ¬
process "Safari" whose name = "Reload this page")
delay 0.5
end repeat
log "page is LOADED"
end waitForSafariPageToFinishLoading

how do we reset the size of safari devtools with applescript for ios simulators?

how do we reset the size of safari dev-tools with apple-script for simulators? The url can be random and anything(any site). As can be seen from the screenshot, there is a setting option we get in safari devtools for simulators, where we can set the devtools size with "zoom" option. I wanted it to set to 100% with applescript doing the same job.
This script i was trying as shown below is trying to activate safari, then go the simulator device and click on the url. Then i am stuck on how to click on the settings tab on safari devtools and finally on zoom option to resize devtools with applescript
Below is the image:
Please see this image
tell application "System Events"
tell application process "Safari"
if not (exists menu bar item "Develop" of menu bar 1) then return
tell menu 1 of menu bar item "Develop" of menu bar 1
set simulatorMenuName to the name of (menu items whose name starts with "Simulator") as string
if simulatorMenuName is equal to "" then return
set simulatorMenuNameMenuItems to the name of menu items of menu 1 of menu item simulatorMenuName
if item 1 of simulatorMenuNameMenuItems is not "Safari" then return
repeat with i from 1 to count simulatorMenuNameMenuItems
if item i of simulatorMenuNameMenuItems is equal to missing value then
set menuItemNumber to i - 1
exit repeat
end if
end repeat
tell menu 1 of menu item simulatorMenuName to click menu item menuItemNumber
end tell
end tell
end tell
I am not able to test in the same environment as you at the present time, however, he example AppleScript code, shown below, was tested in Script Editor under macOS Catalina and Safari version 14.0.3 (15610.4.3.1.6, 15610) with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.
1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.
Example AppleScript code:
tell application "Safari" to activate
delay 0.2
tell application "System Events"
tell front window of application process "Safari"
if (value of pop up button 1 of group 19 of ¬
group 4 of UI element 1 of scroll area 1 of ¬
group 1 of group 1) is not "100%" then
click ¬
pop up button 1 of group 19 of ¬
group 4 of UI element 1 of ¬
scroll area 1 of group 1 of group 1
delay 0.1
click ¬
menu item "100%" of menu 1 of group 1
end if
end tell
end tell
Notes:
The example AppleScript code assumes that the front window of Safari is already loaded to the target page and or Web Inspector tab/window.
The target tab of the Web Inspector does not appear to be JavaScript scriptable via AppleScript, however, it can be scripted using UI Scripting. Note that UI Scripting is kludgy at best and prone to failure due to changes in the hierarchical UI element structure of the OS and or application being scripted. Changes to the example AppleScript code presented may need to be made to your use in macOS Big Sur, or any other version of macOS and or Safari other than what it was tested and worked under.
You can use Accessibility Inspector to check the hierarchical UI element structure under the current version of macOS and Safari your are using. You can also use AppleScript to query the various hierarchical UI element structures to ascertain which it the right one for the pop up button button.
Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

Cocoa application contains no menu bars, according to AppleScript

I am creating a mac app that needs to start dictation (OSX 10.8) by itself. Because there is no way to initiate dictation "directly" the best way to do this is through the menu bar "Edit"/"Start Dictation" because a user may have different keyboard shortcuts for it.
Here's the simple script my app calls (using an NSAppleScript object):
tell application "System Events"
tell application process "MyApp"
tell menu bar 1
tell menu bar item "Edit"
tell menu "Edit"
click menu item "Start Dictation"
end tell
end tell
end tell
end tell
end tell
Here are the results (NSLog'd the error from the AppleScript)
Error:-1719 System Events got an error:
Can’t get menu bar 1 of application process "MyApp". Invalid index.
I did a basic test to see what was going on
My App:
tell application "System Events"
tell application process "MyApp"
set x to menu bars
return x
end tell
end tell
result: <NSAppleEventDescriptor: [ ]>
Finder:
tell application "System Events"
tell application process "Finder"
set x to menu bars
return x
end tell
end tell
result: <NSAppleEventDescriptor: [ 'obj '{ 'form':'indx', 'want':'mbar', 'seld':1, 'from':'obj '{ 'form':'name', 'want':'pcap', 'seld':'utxt'("Finder"), 'from':null() } } ]>
So basically AppleScript is telling me my app has no menu bars? I run Accessibility Inspector and sure enough there is in fact a menu bar (plus I can see it...).
What's going wrong here?
I was after something similar, I wanted to activate a service from within Emacs which had otherwise overridden the global keyboard shortcut to the service - A different question which I will ask in another thread.
Running your examples above I did discover that I had to have added the application (Emacs and for testing Automator) to the Accessibility applications (System Preferences -> Security & Privacy -> Privacy -> Accessibility). Further information regarding that on select-a-menu-item-in-applescript-without-using-system-events-in-10-9-maverick and also AppleScript - uiscripting.
With that access in place I am able to see the menu items under services:
tell application "Emacs"
activate
end tell
tell application "System Events"
tell application process "Emacs"
tell menu bar 1
tell menu "Emacs"
tell menu item "Services"
tell menu "Services"
set x to menu items
click menu item "XXX"
return x
end tell
end tell
end tell
end tell
end tell
end tell
But the action is not activated!
Using the example above an error is returned that 'XXX' does not exist. Replacing the string with the correct string means no error occurs, but the action does not take place.
Have you tried activating your app with a short delay before running the system events stuff? You're also missing some other stuff. Here's how you would do it in Safari...
tell application "Safari" to activate
delay 1
tell application "System Events"
tell process "Safari"
tell menu bar item "Edit" of menu bar 1
click
delay 0.5
tell menu item "Speech" of menu 1
click
delay 0.5
click menu item "Start Speaking" of menu 1
end tell
end tell
end tell
end tell
A second point. If you are creating the application yourself then you do not need to do as you are suggesting. You state "Because there is no way to initiate dictation directly" but why do you say that? Every menu item is hooked up to a command, so in essence you can hook into that command if this is your own application. Just create a button or something in your interface and connect it to the same command that the menu item is connected to. For example I can see in Xcode that the "Start Speaking" menu item is connected to First Responder's startSpeaking command. As such you can create a button or some other item and connect it to startSpeaking of First Responder in Interface builder yourself.

How to programmatically open Safari->preferences pane

I m working on safari plugin. I have added some Qt code in it. Now I want to open "safari->preferences" pane when user clicks on QPushButton on my Qt dialog.
If it is possible in Objective-C also, please tell me.
Finally I got it.
Here is my solution:
tell application "Safari" to activate
delay 4
tell application "System Events" to tell process "Safari"
keystroke "," using command down
tell window 1
click button "Extensions" of tool bar 1
activate "Extensions"
keystroke return
end tell
end tell
Using this AppleScript we can open Safari->Preferences programatically.
I dont know a direct api.
write an apple script and use ui scripting
to tell safari
1) to open
2) to select the menuitem preferences (maybe even send key stroke cmd+,)
some script like:
tell application "System Events"
tell process "Safari"
tell menu bar 1
tell menu bar item "Safari"
tell menu "Safari"
click menu item "Preferences..."
end tell
end tell
end tell
end tell

Is there a way to start Safari with the Web Inspector open?

I have to close and reopen it for development purposes like 100 times a day and the extra time it takes to open the web inspector disable cache, clear cache, start debugging javascript, and occasionally clear local databases is getting to be tedious.
Here is an AppleScript I wrote that launches Safari Inspector. You can export it as an executable application and have it sitting in your dock to get into Inspector with a single click. You could also launch it in a build phase in Xcode to have it run when your app is sent to simulator. Change "iPad Simulator" to "iPone Simulator" or connected device as needed.
tell application "Safari"
activate
delay 2
tell application "System Events"
tell process "Safari"
set frontmost to true
click menu item 2 of menu 1 of menu item "iPad Simulator" of menu 1 of menu bar item "Develop" of menu bar 1
end tell
end tell
end tell