set application to frontmost in applescript - applescript

I'm trying to find out what the frontmost application x is, run a script, then set x to the frontmost application using applescript.
tell application "System Events"
set frontApp to displayed name of first process whose frontmost is true
set apptemp to frontApp
end tell
[code here]
tell application "System Events"
set frontmost of process whose name is apptemp to true
end tell
Although this code does not return any bugs it does not work. I've also tried this code
tell application "System Events"
set apptemp to application "Google Chrome"
set frontmost of process "Google Chrome" to true
end tell
But again, although there are no bugs it will not work.
Also, someone please tell the admin to make it easier to display code. I have the most difficult time displaying code on this website. I have to indent four spaces for each line of code, that's insane.

Even better than both other approaches is using the bundle identifier which is a unique key.
tell application "System Events"
set frontAppID to bundle identifier of first process whose frontmost is true
end tell
-- Sample code... can be anything else
activate application "Finder"
delay 3
-- End of sample code
activate application id frontAppID

A robust way to handle this is to use path to frontmost application as follows:
# Save which application is frontmost (active),
# as an absolute, HFS-style path string pointing to the application bundle.
set frontmostAppPath to (path to frontmost application) as text
# Activate and work with another application.
activate application "Reminders"
delay 2
# Make the previously frontmost (active) application frontmost again.
activate application frontmostAppPath
Using the application's specific path ensures that the very same application is reactivated, irrespective of duplicates and applications whose process names differ.
Note: It's tempting to want to save and restore the object reference frontmost application directly, but that doesn't actually work: inexplicably, such a saved reference is treated the same as the current application (the one running the code).

Related

Using AppleScript automation how can I get currently opened Adobe reader file name

I tried below code
activate application "Adobe Reader"
tell application "System Events"
tell process "Adobe Reader"
set currentFile to active Document
end tell
end tell
But I couldn't get active document name, I tried this code by already with opened document. I don't even find any dictionary for adobe reader in script Editor. Any suggestions will be much appreciated
The Adobe Reader app does not have an AppleScript dictionary file within it's application bundle and as such is not AppleScript scriptable beyond a very limited number of Standard Suite commands and UI Scripting its menu bar with System Events.
If you just want to get the name of the document that has focus, then the following example AppleScript code can do that:
if running of application id "com.adobe.Reader" then
try
tell application "System Events" to ¬
set docName to ¬
(get name of every menu item of menu 1 of menu bar item ¬
"Window" of menu bar 1 of application process ¬
"Acrobat Reader" whose value of ¬
attribute "AXMenuItemMarkChar" = "✓") as string
return docName
end try
end if
The use of return docName was for testing purposes and can be removed as appropriate. Additionally, it was not necessary for Adobe Reader to have focus and its window could even be minimized, the script still retrieved the name as shown on its Window menu.
Note: This was tested on macOS High Sierra using US English and Adobe Acrobat Reader DC (Continuous Release | Version 2019.021.20058.) and works as is. Adjustments may be needed for other languages and or different versions of Adobe Reader.
Note: The example AppleScript code is just that and 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.
Here are a few AppleScript options that may work for you. I don’t have Adobe Reader so I am not able to test the code.
tell application "Adobe Reader" to set currentFile to name of document 1
If that doesn’t work for you, you can try this…
tell application "Adobe Reader" to activate
repeat until application ""Adobe Reader" is frontmost
delay 0.1
end repeat
tell application (path to frontmost application as text) to set currentFile to name of document 1

Notified when AppleScript command finishes

I have the following AppleScript:
tell application "Finder"
open file "MyApp.xcworkspace" of folder of (file (path to me))
end tell
tell application "System Events"
delay 5
keystroke "u" using {command down}
end tell
Basically the script launches XCode and executes the tests. The problem is that the script is marked as finished as soon as the last command is executed. That means the tests start and there is no way to find out whether they are finished or not.
Any ideas about how to determine whether the tests are finished or not ?
Are you aware that Xcode is scriptable?
tell application "System Events" to set myWorkspace to path of file "MyApp.xcworkspace" of container of (path to me)
tell application "Xcode"
open myWorkspace
set activeDoc to 1st workspace document
tell activeDoc
test
repeat until completed of last scheme action result
delay 0.5
end repeat
set lastTestResult to last scheme action result
end tell
end tell

Dynamic Slideshow using Applescript

I have a folder of images that gets updated from a camera taking pictures periodically throughout the day. I'm trying to write some applescript that will create a slideshow from a folder of images but also update as more are added without having to rerun the script. I started out trying to do quick look but couldn't get that working. Any ideas on how best to tackle this?
UPDATE
this is what I have hacked together so far:
tell application "Finder" to set the_folder to get folder (choose folder)
tell application "Finder" to open the_folder
tell application "System Events"
tell process "Finder"
key code 124
keystroke "a" using command down
keystroke " " using option down
end tell
end tell
I don't believe this works if I add photos behind the scenes though.
This is what I came up with and it works perfectly for what I need.
tell application "Finder" to set the_folder to get folder (choose folder)
tell application "Finder" to open the_folder
tell application "System Events"
tell process "Finder"
key code 124
keystroke "a" using command down
keystroke " " using option down
end tell
end tell
repeat while true
delay 60
tell application "System Events"
tell process "Finder"
keystroke "a" using command down
end tell
end tell
end repeat
A few caveats... since it is using quick look, you can't really do anything on the computer while this is running since it can't have any other app activate while it is running (quick look closes when this happens). Also the repeat section is required to get quick look to pickup the new additions to the directory without losing focus. Pretty nasty stuff, but I couldn't really find another easy way to do it!

Bring application to front by ID

I want to use AppleScript to bring an app to the front. If I run the following script
tell application "System Events"
tell process id 916
activate
end tell
end tell
the process doesn't come to front. Instead, only the active window of the currently front-most app loses focus, but that app stays in front.
Is it even possible to do this with a process ID rather than an application name? I have tried this on Mac OS X 10.6.8 and 10.7.5.
I am looking for a plain AppleScript solution. It should not use any shell commands or any other solution. I want to use the process ID number because I might have running multiple instances of the same application (in the same file location).
I have found the following solution:
tell application "System Events"
set myProcesses to every process whose unix id is myPocessID
repeat with myProcess in myProcesses
set the frontmost of myProcess to true
end repeat
end tell
Foo's answer works too:
tell application "System Events"
set frontmost of every process whose unix id is myProcessID to true
end tell
set processID to 432--currently firefox for me
tell application "System Events" to set a to file of 1st item of (processes whose unix id = processID)
activate application (a as alias as string)
This uses the path to the app file, which is apparently necessary (not just the name).
I have another answer which uses do shell script; I could add that if you want.

AppleScript -> Activate window of a non-scriptable application

I have opened 2 "Finder" window A & B, A is in the front while B underneath, the following snippet brings B to the front the topmost:
tell application "Finder"
activate
activate window 2
end tell
But for applications that do not support scripting, the code just mentioned won't help.
Any ideas for activating a window of non-scripting application.
You can usually turn to system events in these cases. System events knows about the windows of running processes and you can usually manipulate those windows. Something like this will show you some of the things you can do. Just play around with the code and see if you can do what you want.
tell application "System Events"
tell process "Whatever"
properties of windows
end tell
end tell
EDIT: One of the properties of a window is its "title". So you might be able to use that. This approach uses the fact that many applications have a "Window" menu and under that menu many times the name of the windows are listed and you can switch windows by clicking the approprite menu item. So something like this might work... my example uses TextEdit.
tell application "TextEdit" to activate
tell application "System Events"
tell process "TextEdit"
set windowTitle to title of window 2
click menu item windowTitle of menu 1 of menu bar item "Window" of menu bar 1
end tell
end tell
What is your definition of non-scriptable? Just about everything is scriptable to some degree, but for the sake of an example lets use, does not contain an AppleScript dictionary, e.g. AppName.sdef within its application bundle.
For example, the macOS included Stickies application does not contain the Stickies.sdef file, and when trying to add it to the Library in Script Editor is says, "Unable to add the item because it is not scriptable."
In a case such as this, then System Events is needed to talk to the application process, e.g.:
Example AppleScript code:
if running of application "Stickies" then
tell application "System Events"
tell application process "Stickies"
set frontmost to true
if exists window 2 then ¬
perform action "AXRaise" of window 2
end tell
end tell
end if
Notes:
I've included error handling in the example AppleScript code, which can be removed if you prefer.

Resources