Controlling Instruments using applescript - applescript

Is that possible to control or automate apple’s Instruments through applescript?
If Yes, pls let me know its procedure.
Thanks in advance.

YES! I just did that this week.
Here is a sample script that lets you target the Simulator. To target Instruments, you would have to turn on GUI scripting and go through that interface. I used the UI Browser to point out to me which control I needed to bring into scope.
This script sends a return to text field 1 in window 1 of the app running in the Simulator. You would use a similar approach to script Instruments.
on run
main()
end run
on main()
tell application "System Events"
set myWindow to get window 1 of application process "iOS Simulator"
set myField to text field 1 of myWindow
end tell
set iterations to 0
repeat while true
activate application "iPhone Simulator"
set iterations to iterations + 1
log iterations
tell application "System Events"
click myField
keystroke return
end tell
delay 10
end repeat
end main

Instruments is not scriptable with Applescript.

It depends on the task you are trying to perform - some things in instruments can indeed be scripted. If you could provide a little more context then maybe we can help...

Related

Is there any other way to get foreground window title in macOS other than AppleScript

I want to get the foreground window title in macOS.
I have tried using AppleScript for this, it works but is very slow.
Here is the AppleScript code:
tell application "System Events"
set frontApp to name of first application process whose frontmost is true
end tell
tell application frontApp
if the (count of windows) is not 0 then
set window_name to name of front window
end if
end tell
It takes a lot of time when we run this using Java.
Is there any other efficient solution to this?
I haven't used Java in ages, so I don't remember how it accesses frameworks (assuming I ever knew), but the framework you want is Quartz Window Services. These methods give you access to the window server, which manages all of the onscreen windows.
This AppleScript code works for me using the latest version of macOS Mojave.
For me, this AppleScript code is lightning fast.
try
tell application "System Events" to tell (process 1 whose it is frontmost) ¬
to tell (window 1 whose value of attribute "AXMain" is true) ¬
to set windowTitle to value of attribute "AXTitle"
end try

AppleScript to block Safari pop-up windows

I am trying to automate the blocking of pop-up windows in Safari.
I have tried the following defaults write operation
defaults write com.apple.Safari WebKit2JavaScriptCanOpenWindowsAutomatically -boolean false
But this fails to do it. I have also tried setting it as true but even that didn't help. I am currently trying to find a way to do it using AppleScript.
This is what I have written so far -
CMD_ACTIVATE='tell application "Safari" to activate'
CMD_NEWTAB='tell application "System Events" to keystroke "," using {command down}'
osascript -e "$CMD_ACTIVATE" -e "$CMD_NEWTAB"
This opens up the preferences but then I draw a blank. Anyone with any suggestions on how to proceed? Also I don't really need a solution to this only in AppleScript any other way to do it would also be helpful.
Note: I have been using Mac OS only for a week now, and am not that well versed in the nuances of this OS, so please be a bit descriptive when answering.
Thanks.
Blocking pop-up window is done via Safari / preferences / tab Security where you need to set the correct checkbox.
This preference seems to be stored in your library / preferences file com.safari.plist which contains the flag com.apple.Safari.ContentPageGroupIdentifier.WebKit2JavaScriptCanOpenWindowsAutomatically with value Yes when pop-up are blocked.
However, it may not be the only place to be changed and in any cases, it is not officially documented, so that place can be changed by Apple any time. This is not recommended to use it.
Going back to Applescript, because Safari is quite poor in terms of applescript handling events, you are forced to go via GUI scripting. That’s what you’ve started, but keep in mind that if Apple changes the layout of Safari preference window, your script must be reviewed.
When going through GUI scripting (which, again, should only be when no other solution found) you must understand structure of GUI objects. Window contains button, check box, tool bar... in a hierarchy model. For instance the preference window in Safari contains bellow the tool bar, an object "group 1" with itself contains many objects depending of the tool bar current selection. Once you understand that concept, the script below, which does what you're looking for, will be easy to understand with many comments:
tell application "Safari" to activate
tell application "System Events"
keystroke "," using {command down}
delay 0.2 -- leave sometime to open window
tell window 1 of process "Safari"
click button 6 of toolbar 1 -- Security button is number 6
delay 0.2
-- check if check box not yet set and set it.
if (value of checkbox 5 of group 1 of group 1) = 0 then click checkbox 5 of group 1 of group 1
end tell
delay 0.2
click button 1 of window 1 of process "Safari" -- click on red / close button
end tell
I am running on Safari 10.0.3. If your version is different, the preference window may be different. Then the script must be adjusted: the Security tab button may not longer be the number 6 in your version,...

Automatically Add Application to Allow assistive access

Part of an application changes the scroll direction of the trackpad with this AppleScript (used as AppleScriptObjC in Xcode AppleScript application).
When running, it often pops up with a message telling me that my app has not been allowed accessibility access (the usual message...)
Here is the code:
on TrackpadIsAttached()
try
tell application "System Preferences" to quit
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.trackpad"
end tell
tell application "System Events" to tell application process "System Preferences"
tell radio button 2 of tab group 1 of window 1 to if value is 0 then click
tell checkbox 1 of tab group 1 of window 1 to if value is 1 then click
end tell
tell application "System Preferences" to quit
functionSuccessful("Mouse control optimisation completed successfully.")
on error errMsg
my errorReporting(errMsg, "Fatal Mouse Optimisation (Trackpad) Error")
tell application "System Preferences" to quit
end try
end TrackpadIsAttached
I know I can do it manually with: System Preferences > Security & Privacy > Privacy > Accessibility but can it also be done automatically with AppleScriptObjC or 'Do Shell Script'? - I don't mind if the user has to type is a password to authenticate it etc.
Any help is greatly appreciated.
Two things:
GUI Scripting is the automation option of absolutely last resort: humiliatingly klunky and brittle as hell, and enabling it either manually or automatically opens up a potential security risk—not a liability you want to place on either yourself or your users if you can possibly help it. If there is any programmatic way to perform the operation you need, use that instead. Which leads us to…
You haven't said why your users need and/or want to change their trackpad direction. What is the purpose of your app? Is it intended to give users who frequently reverse trackpad direction (e.g. while playing third-party games) with an easier way to do it? Or it trying to reverse it for its own purposes? (e.g. If your app just needs to know which way the user's finger is really moving, use -[NSEvent isDirectionInvertedFromDevice].)
Update your question to explain your overall goal, and you're a lot more likely to receive robust high-quality answers that won't further dig you into any holes of your own unintended making.

make applescript auto-click

I play a game in Safari.
I want to make an applescript that auto clicks for me in a special spot on the screen, I have tried many things but none of it has worked for me.
Any ideas how to do it?
You can record the mouse movement with Automator and play this record: you can save it as application, too...
It's the easiest way, in my opinion...
delay 3
tell application "System Events"
tell application process "TheApp"
key code 53
delay 1
click (click at {1800, 1200})
end tell
end tell
However, it doesn't seem to work in Mavericks.
I was trying to achieve the same thing as you a few months/years ago :)
I was botting(the thing you want to achieve) a game named Runescape =P
Of course it is against the rule so you may get banned :S
As sylter says you can use Automator to automate your task (which is the only solution) i tried to click the mouse button but i was not able to find out how :/
you should use automator! it is REALLY easy :)
repeat 10 times
delay 1
tell application "System Events"
tell application process "Safari"
click (click at {650, 730})
end tell
end tell
end repeat
should work for you

AppleScript: How do I launch an application and then execute a menu command

I am trying to create a small applescript which will launch the DVD Player application and then resize it to its Actual Size. The code is listed below. When I run the code the DVD Player launches but it does not resize the window. What am I missing to get this to work?
Thanks,
Rob
do_menu("DVD Player", "View", "Actual Size")
on do_menu(app_name, menu_name, menu_item)
try
-- bring the target application to the front
tell application app_name
activate
end tell
delay 3
tell application "System Events"
tell process app_name
tell menu bar 1
tell menu bar item menu_name
tell menu menu_name
click menu item menu_item
end tell
end tell
end tell
end tell
end tell
return true
on error error_message
return false
end try
end do_menu
Have you looked at the dictionary for DVD Player?
There are properties for adjusting the size.
This will open it and go full screen:
tell application "DVD Player"
open
delay 2
set viewer full screen to true
end tell
Or there is the viewer size property which states:
viewer size (half/normal/double/max) : To set the the viewer size
So you could use this to go to Actual Size:
tell application "DVD Player"
open
delay 2
set viewer size to normal
end tell
Is that what you wanted to do?
I'm not in front of a Mac so I can't really test your code, but my suggestion is to try an existing and proven implementation of the same functionality, which uses recursion rather than nesting:
http://hints.macworld.com/article.php?story=20060921045743404
It's also possible that your delay 3 is not long enough for the DVD Player application to completely load before you start trying to script menu events. You could try debugging this by running two separate scripts and see if your menu-activation code works once the DVD Player app is loaded.

Resources