I have this AppleScript that returns the name of the currently playing artist in iTunes. Unfortunately, if the artist name contains special characters, like ú, bizarre characters are output. How can I fix this?
tell application "iTunes"
if player state is playing then
try
set myTrack to artist of current track
on error
return ""
end try
return myTrack
end if
end tell
Unicode characters shouldn't normally cause any problems:
tell application "iTunes" to tell current track
set name to "あ"
name -- あ
end tell
The ID3 tags of some of your files probably have some non-Unicode encoding. I don't know how to deal with that from an AppleScript, but could you try converting the tags to UTF-8? See https://superuser.com/questions/90449/repair-encoding-of-id3-tags or http://code.google.com/p/id3-to-unicode/.
Related
I'd like an easy way to switch from a Spotify release to the same release in Apple Music.
I already found a way to search for the currently playing Spotify track in the Apple Music web player with Applescript:
tell application "Spotify"
if player state is not stopped then
set currentArtist to artist of current track as string
set currentTrack to name of current track as string
open location "https://music.apple.com/search?term=" & currentArtist & " " & currentTrack
end if
end tell
I'd love to:
Open the search in the native Music.app, not the web player. Is this supported?
Ideally not do a search, but go straight to the same release. Maybe with ISRC codes?
Take any selected Spotify track, not just the currently playing one. Looking at the Spotify Applescript dictionary tells me this in not possible.
had a similar problem right now and quickly hacked it out. In my case I want to simply trigger a search on the Music app.
Opened Automator and created a new "Service".
Workflow receives current > "Text" > in "every application".
Here's the AppleScript:
on run {input, parameters}
tell application "Music"
activate
end tell
tell application "System Events"
tell process "Music"
set window_name to name of front window
set value of text field 1 of UI element 1 of row 1 of outline 1 of splitter group 1 of window window_name to input
keystroke ""
key code 36
end tell
end tell
return input
end run
I saved it as "Find on Music" in Automator and now I can select text, right click > Service > Find on Music and Music plops open and shows me the results for the selected text. I hope you can use some parts of it.
I just figured out how to pass text from wherever to the search field in Music, with help from daemon's answer, which no longer works. This should work for what you want to do in conjunction with what you have.
Replace your "open location" line with a variable name for your concatenated string. Add this code below yours and pass that variable in place of 'input' (in my case 'input' is text from any application, which I use to select text of an artist name in an email/webpage/message that I want to send to Music's search).
First it checks to see if the main Music window is open vs the MiniPlayer, and open it if not to enable search via cmd-O, the cmd-F to find, then passes the input and hits return:
tell application "Music"
activate
end tell
tell application "System Events"
if not (exists (window "Music" of process "Music")) then
tell process "Music"
keystroke "0" using command down
end tell
end if
tell process "Music"
keystroke "f" using command down
keystroke input
key code 36
end tell
end tell
So, something like this (I don't have Spotify to check that section, but this should work assuming your code there is correct):
tell application "Spotify"
if player state is not stopped then
set currentArtist to artist of current track as string
set currentTrack to name of current track as string
set spotTrack to currentArtist & " " & currentTrack
end if
end tell
tell application "Music"
activate
end tell
tell application "System Events"
if not (exists (window "Music" of process "Music")) then
tell process "Music"
keystroke "0" using command down
end tell
end if
tell process "Music"
keystroke "f" using command down
keystroke spotTrack
key code 36
end tell
end tell
The only thing I couldn't figure out is how to check if the search field is already in focus, because if it is, the cmd-F causes a system alert sound. Generally not an issue as typically you'll search and interact with something else before running this script again, so calling it good. :)
How can I insert text from Chinese characters using AppleScript? All my attempts end with "aaaa aaaaaa aaaaaaa", I tried to find some information about using Unicode, but I failed.
I also tried to change the system language. This relatively helped, AppleScript actually started inserting hieroglyphs, but in the final result it didn't help, because only 1 hieroglyph was printed due to auto-correction and help in typing.
I'm making some guesses about what you're trying to do but this should allow for inserting text into a document (or the clipboard). I use TextEdit but it should work with any app that allows you to work similarly with text. In my example, I simply use your text as is, but you could get that text from the clipboard or another document (as provided for in the comments).
set srcIdeo to "你好,朋友!你好嗎?测试消息。" -- text taken from screen shot
--set srcIdeo to the clipboard
tell application "TextEdit"
-- list all Chinese characters to begin with
-- set srcIdeo to every character of document "sourceIdeo.txt"
set allChar to every character of srcIdeo
-- list unicode id of allChar
set chList to {}
repeat with i in allChar
copy id of i to end of chList
end repeat
end tell
-- list all Chinese characters to insert into text (doesn't work inside TextEdit tell block)
set chTx to ""
repeat with ch in chList
character id ch
set chTx to chTx & character id ch
end repeat
-- set the clipboard to chTx
-- insert into text
tell application "TextEdit" to set last paragraph of front document to chTx
--tell application "TextEdit" to set last paragraph of front document to the clipboard
(*
Example
allChar {"你", "好", ",", "朋", "友", "!", "你", "好", "嗎", "?", "测", "试", "消", "息", "。"}
chList {20320, 22909, 65292, 26379, 21451, 65281, 20320, 22909, 21966, 65311, 27979, 35797, 28040, 24687, 12290}
chTx "你好,朋友!你好嗎?测试消息。"
*)
Essentially, applescript uses a 16-bit dec (base10) code to represent the unicode 4-digit hex code. So for the first character '你', which the character viewer describes as below, the code U+4F60 translates to '20320' — which turns out to be a straight conversion from the hex to decimal. As an aside, it handles RGB colour codes in a similar manner.
CJK UNIFIED IDEOGRAPH-4F60
Unicode: U+4F60, UTF-8: E4 BD A0
In applescript…
id of "你"
character id 20320
From the AppleScript Language Guide, 124
Class Reference > text > Properties of text objects
https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_classes.html
Short answer, we can input non-ASCII characters with the clipboard:
tell application "System Events"
set textBuffer to "你好吗,朋友!"
repeat with i from 1 to count characters of textBuffer
set the clipboard to (character i of textBuffer)
delay 0.05
keystroke "v" using command down
end repeat
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.
I am writing some text in to word file i want to change the color of that text any one can help on that one plz.
I want to print the 'message' from following script in red color.
Here is the Script:
set message to "mostly these windows are popup in application"
on ResultCreationFuction(message)
try
set text_to_save to message as text
tell application "System Events"
tell application "Finder"
set sortedList to sort (get files of folder "SofTestAutomationResult" of desktop) by modification date
set FileCount to get count of sortedList
set theFile to (item FileCount of sortedList) as alias
end tell
set file_ref to open for access theFile with write permission
write (text_to_save & return) to the file_ref starting at eof
close access file_ref
delay 2
end tell
end try
end ResultCreationFuction
Some Details:
The file is word which is all ready present on above location having name "10.012.2014_17_4_20.doc" (the name of .doc file is not fix)
What you are attempting is the wrong way to do it.
To manipulate content like that, including formatted text (not plain
text), you need to work within, ideally, a well-scriptable app, like
Pages (or Word, perhaps, but I don't have that on the machine I'm
writing this from).
Don't use System Events if you don't need to. Use the apps with the appropriate AppleEvents/dictionary, etc. If you don't know what I'm talking about, you need to take advantage of the infinite resource known as the web.
"Fuction" is just bad form.
I would suggest doing a lot more reading up on how AppleScript works (or scripting in general), but to start you out, here is a script I just wrote in pages which sets the color of a specific word of the open document after putting text in there:
tell application "Pages"
set body text of document 1 to "hello there mister fancy pants"
set color of word 3 of body text of page 1 of document 1 to {64614, 0, 111}
end tell
If you have Pages, try this by starting with a blank page and running this script. Obviously, you could get rid of "word 3 of" in the 2nd line, and the whole body text will be red.
I hope this makes sense and is of help.
[edit]
I should mention that even TextEdit is scriptable and can open Word documents. Here's an example using TextEdit:
tell application "TextEdit"
set text of document 1 to "hello mister fancy pants"
set color of words 2 thru 3 of text of document 1 to {65535, 0, 0}
end tell
There is a little danger of non-Word apps losing formatting of Word files. But it just seems you are attempting something very simple, and I'm not sure if Word is really necessary here.
You can't add color using the write to eof. You should open the document in Word and then insert the line and add the color. Here's a script that should demonstrate how:
set text_to_add to "mostly these windows are popup in application"
set theFile to ((path to desktop folder) & "10.012.2014_17_4_20.doc") as string
tell application "Microsoft Word"
set theFile to theFile as string -- assuming theFile is an alias or :: path
open file theFile
tell active document
set endOfDoc to end of content of text object -- insert the text to end of document
set theRange to create range start (endOfDoc - 1) end endOfDoc
insert text text_to_add at theRange
set myRange to create range start endOfDoc end (endOfDoc + (length of text_to_add))
set color index of font object of myRange to red
save
end tell
end tell
I am using iTunes to convert a large .aif file to a much smaller .mp3 file. The filename changes each day because I am adding the date to the name. So, a file named "abcxyz 2-2-2014" gets converted in iTunes. After the conversion, I want to delete it from iTunes. I'm trying to use Applescript to search for the file and delete it. I'm trying this:
on deleteTrack(trackName)
tell application "iTunes"
set theTrack to track named trackName of playlist "Library"
set songFile to location of theTrack
delete theTrack
end tell
tell application "Finder" to delete songFile
end deleteTrack
on run
tell application "iTunes"
set result to (file tracks whose name contains "abcxyz")
repeat with t in result
deleteTrack(name of t as string)
end repeat
end tell
end run
I found the deleteTrack routine which works perfectly if you pass it a string like this:
on run
deleteTrack("abcxyz 2-2-2014")
end run
But that requires that I know the exact name of the track, which I don't. The error that I get is "Can't continue deleteTrack" ..with the deleteTrack(name of t as string) line selected in Applescript Editor.
Thanks for any help.
You should re-read the material about AppleScript's handlers. Because your handler call is inside of a tell application "iTunes" block, AppleScript is looking in iTunes for deleteTrack. Try this instead:
my deleteTrack(name of t as string)