Applescript play music from iTunes URL - applescript

The following script will open a track in iTunes
use application "iTunes"
property trackURL : "itmss://itunes.apple.com/us/album/brahms-violin-concerto-in-d-major-op-77-iii-allegro/145533236?i=145533044&uo=4"
open location trackURL
Now, asking "iTunes" to play it does not work because the track is highlighted but not properly selected, i.e., it requires a manual mouse click to select it and play it.
How can I select the highlighted track? Or how could I ask "iTunes" to play the song?! Alternatively, is there a way to add a music to my library from an URL directly?

Disclaimer: I don't have the Apple Music subscription, so the UI on my end may not be exactly the same as yours. However, if I click the "Play" button, I get the little advertisement asking me to sign up for the service, which I assume would just play the music if you had the service. So, these are the steps I've been able to follow to get that box to pop up:
The first, and most convenient from AppleScript, thing to try is just to hit the space bar to start the music playing. This actually works great if I've selected the item manually by clicking on it. However, after open location, it doesn't work, and this appears to be because even though the row is highlighted in the viewer, the actual keyboard focus seems to be on the page itself (the iTunes Store and Apple Music appear to have their entire UI presented as web pages rendered by WebKit). You can verify this by tapping the up and down arrow keys on the keyboard; the page scrolls up and down instead of you switching to adjacent tracks.
My opinion is that this is actually a bug in iTunes; I'd consider the true solution to the problem to be to report this to Apple via the bug reporter. Using open location really should set the keyboard focus to the track you navigated to.
With that said, we can work around it in the short term by simulating a click on the "Play" button. Note that you'll probably need to add your app in System Preferences > Security and Privacy > Accessibility. Note also that this is incredibly fragile, and if Apple ever changes anything in the layout of the web pages they're serving, this whole thing will break. Finally, please note that this code is extremely ugly; the whole thing gives me hives just by looking at it, but it's the only thing I was able to get to work. Side effects of reading this code may include nausea, headaches, and suicidal thoughts. Do not read this code immediately after eating. Consult your doctor before reading this code if you have a history of depression or obsessive-compulsive disorder.
property trackURL : "itmss://itunes.apple.com/us/album/brahms-violin-concerto-in-d-major-op-77-iii-allegro/145533236?i=145533044&uo=4"
property trackTitle : "III. Allegro giocoso, ma non troppo vivace"
tell application "iTunes"
activate
open location trackURL
delay 1 -- give the page a second to load
end tell
tell application "System Events"
tell process "iTunes"
set theRows to the rows of table 1 of UI element 1 of scroll area 1 of group 1 of group 1 of front window
-- "repeat with eachRow in theRows" isn't working. I don't know why. Freaking AppleScript
repeat with i from 1 to the number of theRows
set eachRow to item i of theRows
if exists group 2 of UI element 2 of eachRow then
if value of static text 1 of group 1 of group 2 of UI element 2 of eachRow is trackTitle then
tell group 1 of UI element 2 of eachRow to click
end if
end if
end repeat
end tell
end tell
If Apple ever fixes the bug, of course, we should be able to just:
tell application "iTunes"
activate
open location trackURL
delay 1 -- give the page a second to load
end tell
tell application "System Events" to keystroke space

Related

How to take a portion screenshot by using QuickTime Player and save it to the clipboard in macOS when using Apple Script for automation?

I'm a uni student taking online courses, I take portion screenshots by pressing Command+Control+Shift+4 every time to take notes, then I paste it to Notion. But it's not efficient to select the same area every time, then I found that QuickTime Player will keep the same area for capturing a portion screenshot, so I decided to use Apple Script to complete the operation.
The code I wrote could switch to the application QuickTime Player and then proceed to "New Screen Recording" in the menu by pressing Command+Control+N, but the problem is that sometimes pressing twice space will call out "Capture Selected Window" instead of "Capture Selected Portion". Meanwhile, the location where saving the screenshots will also be changed too though I already ticked "Remember Last Selection", maybe resetting the location to be saved every time is essential.
on run {input, parameters}
activate application "QuickTime Player"
tell application "System Events"
key code 45 using {command down, control down}
delay 0.1
# Switch to "Capture Select Portion" by pressing SPACE
repeat 2 times
key code 49
delay 0.1
end repeat
# Press ENTER to capture a screenshot
key code 35
delay 0.1
end tell
return input
end run
I spent a few hours finding the existed articles but perhaps not that fulfill my needs.
do shell script "screencapture -ci" is as same as pressing Command+Control+Shift+4, and I have to select the portion manually.
do shell script "screencapture -x -R20,20,640,380 ~/Desktop/test.png" needs a precise coordinate. But the coordinate is variable since the window is not fixed, and it needs to be found by using external software, maybe it's a little bit not efficient. If there is no solution for that, I will use it since it's much more convenient than selecting the portion every time.
Sorry about pasting others' code here and saying why they r not suitable for me, I just wanted to make it clear... Thanks for reading here, have a nice day no matter if u r willing to help me ^ ^
The part about pasting screenshots to Notion works, so I didn't paste it here.
I tried writing something, it works but still has some bugs.
For example, the coordinate of the menu "Options" should be initialized depending on the precise location of the bar since I didn't succeed in finding the element by using entire contents.
You can't move your mouse during the use, or it might cause an error:
System Events got an error: Can’t get menu \"Options\" of button \"Options\" of window \"Window\" of application process \"screencaptureui\"." number -1728 from menu "Options" of button "Options" of window "Window" of application process "screencaptureui
When selecting the path where screenshots will be saved, it might take a few seconds to let the program find the menu item Clipboard.
tell application "System Events"
tell process "QuickTime Player"
# Switch to QuickTime Player (already opened expected, no launching time reserved)
activate
# Open "New Screen Recording" from the menu
click menu item "New Screen Recording" of menu "File" of menu bar item "File" of menu bar 1 of application process "QuickTime Player" of application "System Events"
delay 1
end tell
# Capture Selected Portion
tell application process "screencaptureui"
click checkbox "Capture Selected Portion" of window "Window" of application process "screencaptureui" of application "System Events"
# Select Options from the menu (idk why if I replace it with click menu, the clipboard won't be found, needs to be fixed)
click at {817, 824}
delay 0.1
# Save to the Clipboard
click menu item "Clipboard" of menu "Options" of button "Options" of window "Window" of application process "screencaptureui" of application "System Events"
delay 1
end tell
# Enter to capture the screenshot
key code 36
delay 0.1
# Switch to Notion (already opened expected, no launching time reserved)
tell application "Notion"
activate
delay 0.1
end tell
# Paste the screenshot to Notion
key code 9 using {command down}
end tell
Hope this helps someone, if u hv any idea plz feel free to modify it since it still has bugs.

Place cursor in messages app text box

I am looking for a script that will place the cursor in the text field in the Messages App. I have looked for a keyboard shortcut to do this but cannot find one. Can anyone provide a script, or a similar one I can modify.
NB I am not a programmer or very familiar with AppleScript, but have been able to modify scripts that are close to my needs.
I need this as I am trying to make the messages app controllable using the built in dictation feature in Mac OS. I need a script I can assign to a voice command to place the cursor in the text field so that I can then dictate a message.
Many thanks.
If you are using dictation commands, in any application all you need to do is say the command “Show Numbers” and you will see this:
Then you would just say the command “Twenty” which will place your cursor right where you want it… in this case it would be the text field
Also speaking the command “Show Comands” Will open up this window listing tons of dictation commands.
The following was tested and works under OS X 10.8.5 and Messages 7.0.1 and may need to be adjusted for other versions of OS X/macOS/Messages:
tell application "Messages"
activate
tell application "System Events"
set focused of text area 1 of scroll area 4 of splitter group 1 of window 1 of application process "Messages" to true
end tell
end tell
Note: This is coded with the assumption that Messages is already open with an open window. Additional coding will be necessary, in the form of try and or delay and or on error statements as needed and appropriate otherwise.
Here's an example of how I'd code it otherwise, which handles whether or not Messages is open, has its window showing, etc.
on setFocusToTextArea()
tell application "System Events"
if (count of windows of application process "Messages") is equal to 0 then
click UI element "Messages" of list 1 of application process "Dock"
delay 0.25
end if
try
set focused of text area 1 of scroll area 4 of splitter group 1 of window 1 of application process "Messages" to true
end try
end tell
end setFocusToTextArea
tell application "Messages"
if running then
my setFocusToTextArea()
else
activate
delay 2
my setFocusToTextArea()
end if
activate
end tell
Note: If Messages is closed when this script is run, the delay 2 command gives time for Messages to open before the other code runs. The value of the delay command can be adjusted as appropriate for the speed of your system.

Swap between safari windows in a ninja way

The scenario I have is that the new windows opens, and the script executes inside it. I am looking for a way how to come back to the previous active window and did try these 2 ways:
1. Executing keyboard shortcut
tell application "System Events" to keystroke "§" using command down
Note: Unpleasant experience since the slight flashback as a result of window swap.
2. Giving lastWindow personal ID and later bringing it to the front
set lastWindow to id of window 1
...
set index of window id lastWindow to 1
Note: When the lastWindow is changed it becomes visible but inactive state, and requires an additional click on page to make it truly active
I did also try to change visible false of the newly created window, but it results in making it minimized and slowing the speed of background script execution.
Question. So is there a way to create new window and swap back to the last one while in a most "silent" way?
In reference to:
Note: When the lastWindow is changed it becomes visible but inactive state, and requires an additional click on page to make it truly active
The following example AppleScript code will raise the previous front most window to the top having full focus and be active. No additional click necessary.
tell application "Safari"
activate
set thePreviousFrontWindowID to id of front window
make new document with properties {URL:"https://www.google.com"}
delay 4 -- # The delay command is here as a placeholder for whatever you're doing in the new window.
set index of window id thePreviousFrontWindowID to 1
end tell
tell application "System Events" to perform action "AXRaise" of front window of application process "Safari"
It's the last line in this example AppleScript code that you need to get the window all the way to the top fully focused and active.
In reference to:
Question. So is there a way to create new window and swap back to the last one while in a most "silent" way?
Since windows don't make any sound when, for example, running the example AppleScript code above it couldn't be any more "silent" however, as far as minimizing visual distractions, that's so subjective that it almost should not have been asked. The fact is, there is always going to be some level of visual distraction as one moves though the windows of an app, manually or programmatically.
The only suggestion I have is, maybe set the bounds of the new window to that of the previous front window. Then when whatever is happening in the new and current front window is done and you bring the previous front window forward, you will see the entire window and none of the new window that's now behind it.
tell application "Safari"
activate
set thePreviousFrontWindowID to id of front window
set thePreviousFrontWindowBounds to bounds of front window
make new document with properties {URL:"https://www.google.com"}
set bounds of front window to thePreviousFrontWindowBounds
delay 4 -- # The delay command is here as a placeholder for whatever you're doing in the new window.
set index of window id thePreviousFrontWindowID to 1
end tell
tell application "System Events" to perform action "AXRaise" of front window of application process "Safari"
These are just examples to show how to get farther along then you were.

bluetooth applescript auto file transfer

I encountered a problem when I trying to run this applescript.
The purpose of this applescript is when you single click on any file, and run this script, it will automatically transfer this file to a device on the bluetooth named "david". But I encountered a problem in the line of underdashed. The result shows:
"error "System Events got an error: Can’t get scroll area \"Bluetooth Devices\" of window 1 of process \"Bluetooth File Exchange\"." number -1728 from scroll area "Bluetooth Devices" of window 1 of process "Bluetooth File Exchange""
and I don't know why. I am completely a noob to applescript, this is some script wrote by someone else, and I just changed and add a little bit to it.
Can anyone help please?
property device : "david"
tell application "Finder" to set fileAlias to selection as alias
set fileToSend to fileAlias
tell application "Finder" to open fileToSend using application file id "com.apple.BluetoothFileExchange"
activate application "Bluetooth File Exchange"
tell application "System Events"
tell process "Bluetooth File Exchange"
repeat until exists window 1
end repeat
select (1st row of table of scroll area "Bluetooth Devices" of window 1 whose value of text field 1 is device)
click button "Send" of window 1
end tell
end tell
The problem is now solved, thanks "pbell" pointing out the typo in the code.
The code below is a modified version that works for Bluetooth 4.4.4
property device : "vivo X5Pro D"
tell application "Finder" to set fileAlias to selection as alias
set fileToSend to fileAlias
tell application "Finder" to open fileToSend using application file id "com.apple.BluetoothFileExchange"
activate application "Bluetooth File Exchange"
tell application "System Events"
tell process "Bluetooth File Exchange"
repeat until exists window 1
end repeat
select ((row 1 of table 1 of scroll area 1 of window 1) whose value of UI element 2 of UI element 1 is device)
click button "Send" of window 1
end tell
end tell
The problems of this code are
1) it's running too slow.
2) The code involves with GUI so an system update with modified position of several areas will turn this code into crap. (Thanks to pbell)
so is there any alternative way to bypass the GUI and command directly? I guess this will decrease the running time. :)
You have probably 2 issues in your current script :
1) a value "1" is missing in your line ....row of table of scroll area "Bluetooth Devices"...
You probably deleted it by mistake. it should be :
....row of table 1 of scroll area "Bluetooth Devices"....
2) this script uses GUI scripting. It means it simulate the user actions with mouse. these actions are indeed strictly dependent of the application interface (the design of the window, the buttons, ....). all these items are called UI elements (UI=User Interface). So when you are updating your application, if new version displays are not same, it does not work any more.
Bluetooth application is updated with OS updates. I don't know which system version you have, but on El Capitain, the UI elements used in Bluetooth exchange are not the one used in your script.
For instance, to know the device name in the list, it is the static text 1 of UI element 1 of the row. In your current script, you are looking for text field 1 of the row.
Because device name is no longer a property of the row (it is not a property of the UI element 1 of the row), your syntaxe don't work any more.
Which Bluetooth exchange version are you using ?(mine is 4.4.4)

Using AppleScript to set borders in Numbers?

I've successfully managed to get AppleScript to manipulate the background colors in my Numbers document by refering to the background color property. However, I would like to set the borders of a selection or range of cells as well.
I've looked in the Dictionary in AppleScript Editor for a command or property that could help me out here, but I haven't found anything. Is it the case that it's not possible to create borders in Numbers with AppleScript?
There is nothing in the AppleScript dictionary to allow this functionality (which is awful, IMO, but perhaps Apple will add this in the future if enough people complain).
You can use SystemEvents to interact with the interface of Numbers to add the borders:
-- Set the selection range
tell application "Numbers"
make new document
tell front document to tell active sheet to tell table 1
set selection range to range "A3:C6"
end tell
end tell
-- Asks System Events to interact with the interface
tell application "System Events" to tell application process "Numbers"
-- Asks the main window to click on the button called 'Cell' (the name depends of your system language)
tell window 1
click radio button "Cell" of radio group 1
-- Scroll area 4 is the inspector area
tell scroll area 4
-- The first incrementor of the inspector is the borders' one.
increment incrementor 1
end tell
end tell
end tell
If you want to manipulate other elements of the interface, I recommend using the Accessibility Inspector app that comes with Xcode.

Resources