I am using the code below to get the current iTunes stream title. That works great. Just like I need it to be, the script updates every time when the iTunes stream title changes, the variable Title updates itself as well.
repeat
tell application "iTunes"
set Title to current stream title
end tell
delay 2
end repeat
However, because of the ongoing "repeat" function, the rest of the script will not execute. Do you know of any way to still get the variable Title to be updated when iTunes changes its title, but not obstruct the rest of the script? As if it sort of runs in the background? Thank you!
A solution, use an handler (Handlers are also known as functions, subroutines, or methods).
Put the rest of the script in an handler, call this handler from the loop.
Like this :
set oldTitle to ""
repeat
set t to ""
try
tell application "iTunes" to set t to current stream title
end try
if t is not "" and t is not oldTitle then -- the title changed
set oldTitle to t
my doSomething(t) -- call the handler
end if
delay 3
end repeat
on doSomething(Title)
-- put the rest of the script here
end doSomething
Related
I have an issue with my music library.
Some songs I am unable to play because they cannot be found locally.
Here's an example of the error messages I get when playing a specific song:
The song ... could not be used because the original file could not be found. Would you like to locate it?
I can simply press Cancel and the song will be matched via the Apple Music Service.
This allows me to then play the song.
This issue has been discussed here, albeit not in an automated way. Hence, I would like to find an automated solution.
For this, I took the approach of looping through my library by playing each song.
By default, if a song cannot be found, the script automatically skips to the next song.
However, I would like the script to deal with the "file not found" errors and press Cancel.
My current attempt unfortunately does not work:
-- Play first song in library (turn off shuffle and repeat)
set i to 4000 --number of songs in library
repeat while i > 0
tell application "Music" to play (next track)
tell application "System Events"
key code 53
end tell
set i to i - 1
end repeat
How can I force the script to deal with these pop-up errors?
Note: I am also open to any other, more efficient solution to my problem if you have any suggestions. I decided not to go for the Locate option because it takes more time and I will delete any unreferenced songs from my disk at a later stage anyways.
UPDATED VERSION. Following script doesn't play tracks and programatically clicks button "Cancel" when track is corrupted. Like described by OP fixing tracks manually workflow:
tell application "Music"
activate -- required
tell source "Library"
repeat with nextTrack in tracks
try
with timeout of 2 seconds
set theResult to play (contents of nextTrack)
end timeout
theResult
on error
delay 1
tell application "System Events" to tell process "Music" to tell window 1 to if UI element "Cancel" exists then click UI element "Cancel"
end try
stop
end repeat
end tell
end tell
I have the following line of code:
tell application "iTunes"
if player state is playing then
set trackMediaKind to media kind of current track
display dialog trackMediaKind
end if
end tell
When I print trackMediaKind I get the following: «constant ****kMdS»
In iTunes the media kind looks like:
Is there way to make it print Music instead of «constant ****kMdS»?
-Edit-
tell application "iTunes"
if player state is playing then
set trackMediaKind to media kind of current track
log trackMediaKind as string
end if
end tell
I ran the code up via the terminal by typing: osascript myscript.scpt it stills returns: «constant ****kMdS».
You need to change the following line of code from:
display dialog trackMediaKind
To:
display dialog trackMediaKind as string
If you really want it to display "Music" then you need to do something such as:
tell application "iTunes"
if player state is playing then
set trackMediaKind to media kind of current track
if trackMediaKind as string is "song" then
display dialog "Music"
end if
end if
end tell
BTW In the AppleScript Dictionary for iTunes (12.7.*) , looking at the properties of a track, the media kind shows:
media kind (alert tone/audiobook/book/home video/iTunesU/movie/song/music video/podcast/ringtone/TV show/voice memo/unknown) : the media kind of the track
In other words, you'll need to test the results against what could be returned and process it accordingly to your needs/wants if you want to display differently the returned. Also, anytime it returns things like e.g. «constant ****kMdS» try coercing to text with as string or as text.
Update to address the after the fact edit to your question:
While you didn't originally state in the OP you were running your script in Terminal using osascript, nonetheless there seems to be an issue between running the same code as a .scpt file in the two different environments. One would think that what works correctly in Script Editor, it would work correctly in Terminal using osascript, but in this particular case it doesn't.
The workaround in this particular case is to not use the .scpt file format and instead use a plain text format:
As an example, the following code should display a dialog box with "song" when a song track is playing iTunes and saved in Script Editor as Text, e.g. myscript.applescript, and then run in Terminal using: osascript myscript.applescript
tell application "iTunes"
if player state is playing then
set trackMediaKind to media kind of current track
display dialog trackMediaKind as string
end if
end tell
One can also use the AppleScript code in plain text format and make the file executable to be used directly without having to first type osascript on the command line. Save the following example AppleScript code using an osascript shebang.
#!/usr/bin/osascript
tell application "iTunes"
if player state is playing then
set trackMediaKind to media kind of current track
display dialog trackMediaKind as string
end if
end tell
Make executable with chmod in Terminal, e.g.:
chmod u+x myscript.applescript
Then, if it's in the current directory, execute the script using:
./myscript.applescript
Otherwise, use the pathname to it, if it's not in the PATH.
Note: With this method, it is not necessary to use the .applescript extension or any extension. It's a users preference, even though Script Editor uses that extension by default for plain text AppleScript code files.
We have an automation tool for Photoshop, using a control app, which calls Applescripts controlling Photoshop.
One situation is that we must open a RAW image in the CameraRAW plug-in, and then open it in Photoshop. This part is handled, via an applet using System Events. When that applet terminates, we run the processing script for Photoshop.
As some pictures take quite a bit of time to open, we have to make sure that the picture is really open before the script can run. … and that's where I am stuck.
At the moment, I am using the following code which is intended to wait until the image is open (the criterion for "open" is correct (and tested manually), so that's not the issue here).
tell application "Adobe Photoshop CC 2015"
activate
tell application "System Events"
tell application process "Photoshop CC"
--
display alert "Waiting for Window"
--
repeat
try
set wn to name of window 1 as text
try
if (wn contains "(RGB/16") then
set wn to "image is open: " & wn
end if
end try
if (wn contains "(RGB/16") then
display alert "We are there, quitting now… " & wn
exit repeat
end if
end try
delay 1
end repeat
end tell
end tell
--
display alert "Ready for process"
--
-- and here comes the processing code
end tell
I also tried to set a variable which is tested as argument for repeat, and changed when the exit condition is fulfilled.
Trying to create even alerts within the repeat loop, does not lead to any effect; the script ends up in an infinite loop.
It is well possible that I miss the obvious… So, I am grateful for any helpful hint.
Thanks in advance.
I think you have a few small issues with your script that are causing your problem.
You are using name of window 1 when I believe you need name of document 1. With your first try block structured as it was you weren't realizing it was actually giving an error on name of window 1
The name that is returned doesn't contain the color space and bit count, so I've changed the result test to an empty string
Notice the modifications to the try block around getting the document name
I don't believe it's necessary or see a reason to use "System Events" in this case, so I've modified the version below without it.
Example Script
tell application "Adobe Photoshop CC 2015"
display alert "Waiting for Window"
repeat
try
set wn to name of document 1 as text
on error
set wn to ""
end try
try
if wn is not equal to "" then
set wn to "image is open: " & wn
end if
end try
if wn is not equal to "" then
display alert "We are there, quitting now… " & wn
exit repeat
end if
delay 1
end repeat
display alert "Ready for process"
end tell
I know that there are many commands in AppleScript for Spotify such as the simple playpause command, but how would I pull a playlist's information from Spotify, and paste it in a choose from list? I would like it to take all of the songs from wherever you're listening from, and paste them in a choose from list so you can choose which song you would like to listen to. Is this even possible? Can I do something similar?
Also, how would you enable/disable shuffling?
Additionally, is there a way to search Spotify through AppleScript?
I'm not sure if any of these are possible, and Google doesn't have any info on this right now. Does anyone know how to do this?
The Spotify AppleScript implementation does not have specific commands to get at playlist information -- it only exposes "current track" to get info about the currently-playing track and "next track" to play the next track. You can work around this limitation to build an AppleScript array containing all the tracks in a current playlist.
set trackNameList to {}
set trackIDList to {}
tell application "Spotify"
activate
set shuffling to false
set repeating to true
set sound volume to 0
if player state is not playing then
playpause
end if
set trackID to spotify url of current track
repeat while trackIDList does not contain trackID
set end of trackIDList to trackID
set end of trackNameList to name of current track
next track
delay 1 -- otherwise Spotify misbehaves
set trackID to spotify url of current track
end repeat
end tell
tell me to activate
set chosenNames to choose from list trackNameList without multiple selections allowed
set chosenName to (item 1 of chosenNames) as string
repeat with i from 1 to count of trackNameList
set itsName to (item i of trackNameList) as string
if itsName is chosenName then
exit repeat
end if
end repeat
set trackID to (item i of trackIDList) as string
tell application "Spotify"
activate
set sound volume to 100
play track trackID
end tell
There is no AppleScript support for searching in Spotify.
I've got a script than on login, ask a user if they want to watch a movie. For the most part it works great. However on occasion and for reasons unknown to me I get an AppleEvent handler failed error. I've read other post on this error but they all seem to be unique. So, if possible can someone please take a look at my script and tell me why this occasionally pops up and if there's anything i can do to prevent it?
One thing that might help to know, is the one thing in the script that fails when this error occurs is the movie doesn't play. It opens in quicktime but doesn't start.
Thanks in advance, here's the script.
tell application "Welcome" to activate
set question to display dialog "Would you like a welcome video?" buttons {"No, I've seen it", "Yes, please"} default button 2
set answer to button returned of question
if answer is equal to "Yes, please" then tell application "QuickTime Player"
set theMovie to "Macintosh HD:Library:Desktop Pictures:Mac ML Opening Chalkbaord Video.mov"
set openMovie to open theMovie
present openMovie
play openMovie
delay 30
quit
end tell
if answer is equal to "No, I've seen it" then tell application "Welcome"
quit
tell application "System Events"
delete login item "Welcome"
end tell
end tell
My guess is that you probably need a delay between opening and playing the movie. Sometimes the code runs faster than the computer can react. If that's the case then the movie may still be trying to open when the code tells the movie to play... thus the error. As such I added 2 repeat loops which checks for things to make sure they're available before proceeding to the next step in the code. You also need "open file" in the code instead of just "open".
Your approach in your if statements of telling an application to do something is unusual. I wouldn't do that. I would also combine your if statements into one if/else if statement. Anyway, here's how I would write your code (I'm assuming application "Welcome" is the code itself). I hope this helps!
set theMovie to "Macintosh HD:Library:Desktop Pictures:Mac ML Opening Chalkbaord Video.mov"
tell me to activate
set question to display dialog "Would you like a welcome video?" buttons {"No, I've seen it", "Yes, please"} default button 2
set answer to button returned of question
if answer is equal to "Yes, please" then
tell application "QuickTime Player"
activate
set openMovie to open file theMovie
-- delay until the movie opens
set startTime to current date
repeat until exists document 1
delay 0.2
if (current date) - startTime is greater than 10 then return -- a precaution so you don't get stuck in the repeat loop forever
end repeat
present openMovie
play openMovie
-- delay until the movie stops playing
repeat until document 1 is not playing
delay 1
end repeat
quit
end tell
else if answer is equal to "No, I've seen it" then
tell application "System Events" to delete login item "Welcome"
end if