1

I have the following applescript to skip songs in spotify. If I call the script when a fullscreen application is the frontmost, the application will not be visible after the script has finished. It will be in a different space. Is there a way that I can make the frontmost application visible again with applescript?

set front_app to (path to frontmost application as Unicode text)

tell application "Spotify"
   next track
end tell

tell application front_app to activate
Kim
  • 4,593
  • 5
  • 21
  • 18
  • Possible duplicate of http://stackoverflow.com/questions/2305491/applescript-opening-an-app-in-space-number-n – cwd Dec 14 '11 at 19:15

2 Answers2

2

This probably won't get you anywhere, but you could use this script to loop through all your spaces until you see the application you're targeting...

set front_application to current application
tell application "Spotify" to next track
tell application "Finder" to set collapsed of front_application to false --makes sure the app isn't minimized
repeat
    tell application "System Events" to keystroke (ASCII character 29) using {control down}
    display dialog "Correct space?" buttons{"OK"} default button 1 giving up after 4 --don't click 'OK' if the current space isn't showing the target application
    if gave up of the result is false then exit repeat
end repeat
fireshadow52
  • 6,298
  • 2
  • 30
  • 46
  • Thanks for the answer, but I´m looking for an unobtrusive way skip a song, so popping up a dialog is not something I want to do. – Kim Aug 05 '11 at 06:54
  • You could `tell application "System Events" to key code 53` before the `repeat` loop. – fireshadow52 Aug 05 '11 at 16:44
1
set front_app to current application

tell application "Spotify"
   next track
end tell

tell front_app to activate
Flambino
  • 18,507
  • 2
  • 39
  • 58
  • The code looks a lot better, but it behaves the same. If front_app is a fullscreen application it is still not visible after the script has executed. – Kim Aug 04 '11 at 19:27
  • @Kim: Ah, ok. I don't have Lion installed, so I can't check with full-screen apps. How are you launching the script, by the way? E.g. built-in scripts menu, Quicksilver, FastScripts, or something? I'm wondering if maybe the launcher and not the script is stealing focus from the front app and not returning it. Then again it might just be an issue with Lion's full screen apps. – Flambino Aug 04 '11 at 20:25
  • I launch the script from Alfred. I have scripts for next song, previous song, pause etc. It works like a charm as long as the current application is not a fullscreen application. – Kim Aug 05 '11 at 06:40
  • @kim: Odd. Though it wouldn't be new for Apple forgot the AppleScript support. But how about this: Instead of getting the app, get the app's front window, and tell _that_ to activate after the call to Spotify? Again, I can't check myself, but it's worth trying. Alternatively, taking a cue from fireshadow's answers, you can see if System Events has a "current space", and perhaps switch back to that. Maybe? – Flambino Aug 05 '11 at 10:29