I'm new to apple scripting. I'm trying to write a couple of scripts to let you skip forward/back 5 seconds in iTunes using a global hot key. Here's the script for jumping backwards:
tell application "iTunes"
if player state is stopped then return
try
set player position to (get player position) + (-5)
end try
end tell
It works just fine when running from within Automator and iTunes responds by rewinding 5 seconds. And I have successfully saved it as a service and it's sitting in /Library/Services.
However, when I run the script from the Services menu, nothing happens.
Any ideas?
A reboot of my computer fixed the issue.
Related
is there a way to run a simple tell application quit, when a specific application is opened.
For example, if I open spotify, is there away to trigger a script to quit the app.
Thanks!
is there a way to run a simple tell application quit, when a specific application is opened.
Short Answer: Yes
Longer Answer:
Here are a couple of ways that come to mind...
There is a paid application called EventsScripts that among the many events it can react to, one category is Application Events which contains, Application activated, Application deactivated, Application will launch, Application launched and Application quit.
EventsScripts works with both AppleScript scripts and shell scripts.
Have a look at EventScripts. At the time of this post, it's $5.99 at the US App Store, but a free demo is downloadable from the developers website.
Note: I am not affiliated with the developer of EventScripts, just a satisfied user of the product.
Example AppleScript code:
on run eventArgs
set thisTrigger to (trigger of eventArgs)
if thisTrigger is "Application launched" then
set appName to |applicationName| of eventArgs
if appName is "Spotify" then
tell application appName to quit
end if
end if
end run
A free alternative is Hammerspoon, although one may find it is not as easy to implement and use as e.g. EventsScripts.
Here is an example of the code used to watch for the target application has launched and then close it using AppleScript code:
Example Lua code:
function applicationWatcher(appName, eventType)
if (eventType == hs.application.watcher.launched) then
if (appName == "Spotify") then
hs.applescript('tell application "Spotify" to quit')
end
end
end
appWatcher = hs.application.watcher.new(applicationWatcher)
appWatcher:start()
This would be placed in the ~/.hammerspoon/init.lua file and with Hammerspoon running in the background, when the target application is launched, it is told to quit via AppleScript.
Note: I am not affiliated with the developer of Hammerspoon, just a satisfied user of the product.
Was wondering if there is any way to run a small piece of apple script when I launch an application on OS X?
I have a hard-drive which need to be mounted in order for the app to run right. I did this apple script already:
on run
try
do shell script "diskutil mountDisk disk2"
delay 3
tell application "Application"
run
end tell
on error
tell application "Application"
quit
end tell
set question to display dialog "Error." buttons {"OK"}
set answer to button returned of question
if answer is equal to "OK" then
close
end if
end try
end run
But even tho this works great and the hard-drive spins up before the app starts.
I would like to add the diskutil mountDisk disk2 and delay directly in front of the real application when its starting so I can double click files for that app say in my downloads folder. Which will run the script before it opens the clicked file in the app. So I wont have to start the application with another application each time i download a new file for it. Is this even possible?
Shortly:
It's not possible unless the application itself supports some inhook and outhook functionality.
How can I prevent an Apple Script application from crashing whenever the computer logs out or goes to sleep?
I'm currently working on an application that sets the desktop background to the album art of the current song playing in iTunes, however if I put my display to sleep, iTunes will continue to play music but my application will crash, meaning that the desktop background is no longer updating when I return to the computer.
This is the code I'm using to set the wallpaper:
tell desktop 2
set picture to fileName
end tell
where fileName is name of the iTunes artwork of the current song, which has been extracted and saved to the desktop.
It says desktop 2 as I have two different applications for each screen, and conduct testing on my 2nd screen.
This is the error I get when running in Automator and putting the display to sleep:
The action “Run AppleScript” encountered an error.
System Events got an error: Can’t get desktop 2. Invalid index.
I am getting the same results on my primary display also.
How can I stop this script from encountering an error when the display is asleep?
Any suggestions would be greatly appreciated, as I'd love anyone who wants it to have this application.
The safest way is to check if the item exists
if exists desktop 2 then
tell desktop 2
set picture to fileName
end tell
end if
or wrap the code in a try block which ignores any error.
try
tell desktop 2
set picture to fileName
end tell
end try
My environment: Xcode: 7.2.1
Simulator: iOS iPad Air 9.1
Calabash: 0.17.1
Currently, I'm having an issue with my iOS application not touching buttons correctly after I background my application. Here are the general cucumber steps where the issue happens:
When I send the app to the background for 2 seconds
And I skip logging in to MyApp
And I wait and wait
And I should see "Speed"
And I wait for 10 seconds
And I touch "Speed"
Here is the code for the sending the app to background step:
Given /^I send the app to the background for ([\d\.]+) second(?:s)?$/ do |num_seconds|
num_seconds = num_seconds.to_f
send_app_to_background(num_seconds)
end
Basically the touching hits a snag only after I send the app to background. If I take that step out, Calabash touches the button like expected. I included the step where it sees "Speed", just to make sure that Calabash can actually see the button, which it could, since it goes green for that step, as well as the waiting step. But on touching "Speed", Calabash goes into a state where it keeps trying to touch it, but since it doesn't, it hangs on that step indefinitely. I suspect this may be either be a Calabash or UIAutomation bug (https://github.com/calabash/calabash-ios/issues/836), since everything works as expected until or unless I send my app to background, but I wanted to be 100% sure, in case I'm doing something wrong that I'm not seeing.
Does anyone have any ideas as to what the issue may be?
Update:
According to https://groups.google.com/forum/#!topic/calabash-ios/NZAmTp6ckrk, sending app to background is still unimplemented, and when I check the code for send_app_to_background, it does look like it:
def send_app_to_background(secs)
raise 'Not implemented when running without instruments / UIA'
end
Does anyone have a way to work around this for their tests? Running this on a physical device is not an option for me, it has to be done with a simulator.
Update:
Following https://github.com/calabash/calabash-ios/issues/556, I changed sending the app to background code to follow a workaround:
Given /^I send the app to the background for ([\d\.]+) second(?:s)?$/ do |num_seconds|
num_seconds = num_seconds.to_f
uia_send_app_to_background(num_seconds)
end
And the implementation of uia_send_app_to_background is:
def uia_send_app_to_background(secs)
#uia_handle_command(:deactivate, secs)
#Temporary workaround: https://github.com/calabash/calabash-ios/issues/556
js_deactivate = %Q[var x = target.deactivateAppForDuration(#{secs}); var MAX_RETRY=5, retry_count = 0; while (!x && retry_count < MAX_RETRY) { x = target.deactivateAppForDuration(#{secs}); retry_count += 1}; x]
uia(js_deactivate)
end
However, for me, this workaround doesn't seem to work. The app gets pushed to the background, but never goes back to foreground. When I stop that step in irb after a few minutes of waiting, I get this error:
When I send the app to the background for 2 seconds # features/step_definitions/common.rb:91
Could not parse response ''; the app has probably crashed (RuntimeError)
./features/step_definitions/common.rb:93:in `/^I send the app to the background for ([\d\.]+) second(?:s)?$/'
features/display_mapping_units_from_2630.feature:216:in `When I send the app to the background for 2 seconds'
Update
I tried these workaround steps, but touching after backgrounding still doesn't work, and I also tried this on 8.4 simulator, where it doesn't work either, which makes me suspect even more it's an Xcode 7+ issue that needs to be resolved:
When(/^I background the app$/) do
open_app_via_simctl("com.apple.mobilesafari")
sleep(2)
end
When(/^I launch the app$/) do
open_app_via_simctl("com.me.MyApp-cal")
sleep(2)
end
Steps:
And I background the app
And I wait
And I launch the app
And I wait
And I should see "Button1"
And I wait for 10 seconds
And I touch "Button1"
Calabash succeeds in seeing Button1, but when it tries to touch Button1, in 9.1 simulator, Calabash hangs indefinitely, trying to find the button, and in 8.4 simulator, I get an error right away that Calabash can't find the view "Button1".
Issue #836 was closed fixed in November. The Google forum post from 2013 and issue #556 is from 2014. Anything later than a couple months ago should be treated with suspicion.
Don't call the uia_send_app_to_background, that API is broken (by Apple). In general stick with the non-UIA API (don't call uia_* methods unless absolutely necessary).
Update Calabash to latest released version (0.18.1 as of today) and try your tests again.
$ be calabash-ios console
> start_test_server_in_background
# Can touch before background
> touch("view marked:'my view'")
> send_app_to_background(1)
# Can touch after resuming from background
> touch("view marked:'my view'")
The solutions below will not work. Your app will go to the background if you open another app. However, if you open your app with simctl, instruments will no longer have a connection to the application and gestures will fail.
When(/^I background the app$/) do
open_app_via_simctl("com.apple.mobilesafari")
sleep(2)
end
When(/^I launch the app$/) do
open_app_via_simctl("com.me.MyApp-cal")
sleep(2)
end
I have a habit of working/studying while listening to music. And since the Mac I am connected to at work doesn't have all the songs I want, I normally listen to the songs via iTunes from the iPhone Playlists option.
For this, everytime I connect my iPhone to the computer, iTunes detects it, then I have to manually goto the 'iPhone' button in the application, and then "On my iPhone", and then select the playlist, and play a song.
Is there anyway of automating this process ? What I'm looking for, is as soon as I connect my iPhone, iTunes should open the playlist inside my iPhone automatically, and start playing a song at random.
I've tried automating it with Automator. But that doesn't seem to be working. There's something wrong I'm doing in AppleScript ::
tell application "iTunes"
play playlist named "Sasha" of source of type iPhone
end tell
Ha !! I figured it out !! And then I created an applet that would do this for me using AppleScript.
And then, I published the steps/code for the same on my blog below. Here's the link ::
Explanation of Code :: How to start playing songs from your iOS device automatically on your Mac
And the code for the same ::
tell application "iTunes"
set theCurrentPlaylist to view of front browser window
set myiPhone to some source whose kind is iPod
set mainPhonePlaylist to playlist "Sasha" of myiPhone
set the view of the front browser window to mainPhonePlaylist
set EQ enabled to true
play playlist named "Sasha" of myiPhone
set myRandomSong to random number from 1 to count of tracks of (get view of front window)
play track myRandomSong of mainPhonePlaylist
end tell