Notified when AppleScript command finishes - xcode

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

Related

set application to frontmost in 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).

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!

Applescript - won't work when used as a droplet

I'm trying to create a script to use as a droplet where I drop a file onto it, automator opens it in Adobe Acrobat, then applescript runs an action inside of Acrobat to save it as an optimized PDF into a specific folder, then opens that new file in preview and uses a quartz filter to reduce the size of the PDF.
If I have a file already open in Acrobat and just run the script, it executes the action within Acrobat, saves the file, then opens preview (haven't gotten any further). But if I drop the file onto the droplet it just gives me an error message.
Here's the code:
on run {input, parameters}
tell application "System Events"
tell application process "Acrobat"
tell application "Adobe Acrobat Pro" to activate
-- runs the custom action in Acrobat
click the menu item "SAVE in 0-SEND" of menu 1 of menu item "Action Wizard" of the menu "File" of menu bar 1
end tell
end tell
tell application "System Events"
keystroke return
-- clears the action finished message
end tell
tell application "Adobe Acrobat Pro"
activate
close documents saving no
quit
end tell
-- this doesn't work yet
tell application "Finder"
tell application "Preview"
open input
end tell
end tell
end run
I would recommend something along these lines...
on run
set aFile to choose file with prompt "Please select the file to process"
processPDF(aFile)
-- cleanup, quit the application
tell application "Adobe Acrobat Pro" to quit
end run
on open (theFiles)
repeat with aFile in theFiles
processPDF(aFile)
end repeat
-- cleanup, quit the application
tell application "Adobe Acrobat Pro" to quit
end open
on processPDF(theFile)
-- open your file
-- do something with theFile here.
-- close the file
end processPDF

Applescript opening wrong version of OS X app

My script is opening an old version of the app (which I wrote). I have searched the HD and cannot find the old app. The new version is in the applications folder. The script is in the scripts folder.
I made a new folder and put the script and the new app in it. Running the script still opens the wrong app. App2 is the problem child.
Where is the wrong path coming from?
How to:
Point the script at the correct app? I don't want a path in the script.
Find the old app and delete it?
property checkInterval : 5 -- Number of seconds between checks. Adjust to requirement.
global App1WasOpen, GUIScriptingWasEnabled, previousText
on run
-- When the script starts up, note if App1 is running and GUI Scripting is enabled.
tell application "App1" to activate
tell application "System Events"
set App1WasOpen to (application process "App1" exists)
set GUIScriptingWasEnabled to (UI elements enabled)
end tell
-- Enable GUI Scripting if nec. and give the 'previousText' variable an initial value.
switchGUIScripting(true)
set previousText to ""
end run
on idle
-- Each time the script's polled by the system, check that App1's open.
tell application "System Events" to set App1IsOpen to (application process "App1" exists)
if (App1gIsOpen) then
set App1WasOpen to true
-- Get the latest value for 'beam'.
tell application "App1"
set hdg to getHeading
set beam to hdg as string
end tell
if ((count beam) < 3) then set beam to text -3 thru -1 of ("000" & beam)
-- If it's changed since the last check, enter it into App2 and keep it for later checks.
if (beam is not previousText) then
--display dialog "Opening App2" buttons {"OK"}
tell application "App2" to launch
--activate application "App2"
tell application "System Events"
set startTime to current date
repeat until exists (text field 1 of window "App2" of application process "App2")
if (current date) - startTime is greater than 5 then
error "Could not find text field 1 of window AppB of application process App2"
exit repeat
end if
delay 0.2
end repeat
tell application process "App2"
try
set value of text field 1 of window "App2" to beam
end try
end tell
end tell
--tell application "App1" to activate
set previousText to beam
end if
-- Request the next 'idle' event in 'checkInterval' seconds' time.
return checkInterval
else if (App1WasOpen) then
-- App1 was open at the last check, but isn't now. Tell this script applet to quit.
quit
-- It'll actually quit on the next idle event, so request a short interval.
return 1
end if
end idle
-- Unless GUI Scripting was already on when the script started to run, turn it on or off as per the parameter.
on switchGUIScripting(onOff) -- 'onOff' is 'true' for on, 'false' for off.
if (not GUIScriptingWasEnabled) then
tell application "System Events"
activate
set UI elements enabled to (onOff)
end tell
end if
end switchGUIScripting
-- When this applet's told to quit, restore the previous GUI Scripting state before it does.
on quit
switchGUIScripting(false)
continue quit
end quit
Followup:
I used "Show in Finder" on the app's icon in the dock to point me to 17 Xcode archives, which I deleted. Now the script has a recurring message of "An error of type -10660 has occurred", and App2 never opens.
I opened the script editor, copied the code to a new script window and compiled it with a different name. Still doesn't open App2.
Try finding the Applications id with:
set appID to get id of application "/Applications/Safari.app"
Once you have found it, refer to the id rather than
tell application id "com.apple.Safari" to launch

Applescript file dialog with UI scripting

I am trying to open a file in a not so scriptable area of an application.
I got halfway there by using UI scripting to select the proper menu item, but this opens a standard file dialog.
How can I set the destination of the file dialog with Applescript?
ahh ok this should get you going on the right path
tell application "Safari"
activate
-- do menu select gui script first
set posixpath to "/path/to/a/folder/that/exists"
tell window 1
tell application "System Events"
keystroke "g" using {shift down, command down}
keystroke posixpath
delay 1
keystroke return
end tell
end tell
end tell

Resources