Dynamic Slideshow using Applescript - 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!

Related

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

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).

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 keystroke not behaving as expected

I'm experimenting with Applescript for the first time, and am trying to build a script to setup my default layout of applications for developing at work. This involves placing applications across multiple Mission Control spaces. My problem at hand is simply moving about the spaces. I found in many posts similar to this that such action could be achieved with
tell application "System Events"
tell process "Finder"
keystroke "1" using control down
end tell
end tell
if the appropriate key binding was in place. I made the Preferences change so I could use control+1 to move to the first MC space. However, running the script doesn't do anything. This is the event log output:
tell application "System Events"
keystroke "1" using control down
end tell
No errors that I can see, but again: new to Applescript. I've tried many variations of this command including wrapping control down in curly braces and wrapping the call to "System Events" inside a call to "Finder" like this
tell application "Finder"
tell application "System Events"
keystroke "1" using control down
end tell
end tell
but the output is exactly the same with no shift in view. I think I'm missing something here...
According to this question's responses, I tried adding in a delay to make sure I wasn't stepping on my own feet with running the script with CMD-r but nothing happens. I hear the sound effect when you try to click out of an important focus window (if that makes any sense), it's a short beep. Am I talking to the applications improperly?
EDIT
Ok I got something working, but I'm a little confused why this is the case.
tell application "Finder"
activate
delay 0.2
tell application "System Events" to keystroke "a" using control down
end tell
This accomplishes what I need, but I have to change the key binding to a letter. I can replicate the error tone by pressing control+1 when Applescript Editor is active. I guess there's a shortcut for AE that uses the key combo. But why is that running when Finder is supposed to be active?
To answer your question, in your working code the difference is that you activate the Finder before you issue the keystroke command. Keystroke commands are always sent to the frontmost application so you must always make sure to activate an application first as you have done.
If control-1 didn't work then I suspect either some Finder command uses that combo or some other application uses that in a global context meaning it intercepts that command no matter which application is frontmost. Otherwise it should work for you.
Finally, I would remove your system events line of code from the Finder tell block of code. There's no reason the tell the Finder to tell system events to perform a command. Just put that line on its own after the "end tell" line.
Good luck.
I've just been working with this - I don't want to activate finder before triggering what should be a global shortcut. Keystroke was not working, however I tried key code and that indeed works:
on run {}
tell application "System Events"
key code {18} using {command down}
end tell
return "success"
end run
There is a list of key codes at this question: https://apple.stackexchange.com/questions/36943/how-do-i-automate-a-key-press-in-applescript
This should work:
tell application "Finder"
activate
end tell
delay 0.2
tell application "System Events"
key code 18 using {control down}
end tell
I found this tool to be a good alternative that does not have that issue.
Installation is as simple as:
brew install socsieng/tap/sendkeys
Usage:
sendkeys send --initial-delay 0 --delay 0.001 --characters 'Hello'
https://github.com/socsieng/sendkeys

How to set the file in rename mode in apple script?

I know this question is a little bit crazy, but I do want to know the solution. Is there any way to run an apple script to set any given file or folder in rename mode so that the user can type in the new name? I know I can do the rename easily in script but I am just wondering whether it is doable or not.
Thanks in advance!
Well, you can sort of hack it together with System Events
tell application "Finder"
activate
select file "test.png" of desktop
tell application "System Events" to keystroke return
end tell
It's kind of a hack, though, but I don't know if there's another way to do it
It is possible using UI scripting. First select whichever item you're interested in---presumably through AppleScript in practice, but by hand is fine for trying it out. Then:
tell application "Finder" to activate
tell application "System Events" to keystroke return

Resources