bluetooth applescript auto file transfer - macos

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)

Related

Click on a specific place on a window, click on a specific button and control if the window changes

Actually I have 3 questions about the same problem: controlling a window with applescript.
What should I do if I would press on button "Close Window" of application "Google Chrome"?
Is it possible to check if the window changes? For example, to see if appear a pop-up or something like that...
What about clicking on a specific place into a window? I mean, I know I can use
tell application "System Events"
click at {x,y}
end tell
but this command use the entire screen as reference system, and I want it works only on a specific window. For example, if at "{x,y}" i put "{1,1}", applescript will click on the first item on the menu bar. Is there a way I can say to "System Events" to click at "{1,1}", but on the window "Google Chrome"?
Here are three examples of how to close the front window of Google Chrome using AppleScript:
Note: The following assumes Google Chrome is running with at least one window open when you test each example AppleScript code in Script Editor.
Example one is the most straight forward way:
tell application "Google Chrome" to close front window
Example two directly clicks the close button:
tell application "System Events" to tell ¬
application process "Google Chrome" to ¬
click button 1 of front window
Example three calculates the center of the close button and clicks there:
activate application "Google Chrome"
delay 0.5
tell application "System Events" to tell ¬
application process "Google Chrome" to tell ¬
front window
set posB1 to (position of button 1)
set szB1 to (size of button 1)
set x to (item 1 of posB1) + (item 1 of szB1) / 2 as integer
set y to (item 2 of posB1) + (item 2 of szB1) / 2 as integer
end tell
tell application "System Events" to click at {x, y}
Note that in the first two examples, the front window of Google Chrome doesn't even need to be the frontmost window on the Desktop; however, with the third example it does, otherwise the click at {x, y} will not go to the intended target.
That said, example three really shouldn't be used when there it a straight forward way, as in example one, to get the job done. Example three was just a proof of concept to get the coordinates to click at. This method may be useful in some fringe cases, especially in an app that doesn't directly support AppleScript.
Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors.
In applescript GUI scripting you can simply refer to an element by name or index and tell it to click or to perform an action. For instance to click the close button on the first open window in Chrome you could use:
tell application "System Events"
tell process "Google Chrome"
tell window 1
tell button 1
click
end tell
end tell
end tell
end tell
You don't actually need to know its physical position to click one it; you just need to know that the first button in the window is the close button.
System Events always returns the position of any element in screen pixels, so if you want the position of an element in terms of its window, get the position of the element, get the position of the window, and do some addition or subtraction (e.g., if you want to click at {5,5} in a window whose position is {100, 125}, click at {105, 130})
AppleScript isn't really designed to monitor GUI changes, though if you want to be tricky and you know what change you're looking for you can do something like this:
tell application "System Events"
tell process "..."
tell window 1's pop up button 3
repeat until (exists menu 1)
delay 0.2
end repeat
-- menu 1 now exists, so the pop up button is open
end tell
end tell
end tell
...but note that this will hang the script until the menu is opened. A more elegant way to handle that is to write a script application with an idle handler, like so:
on run
-- whatever initialization is needed
end run
on idle
tell application "System Events"
try
tell process "..."
tell window 1's pop up button 3
if exists menu 1 then
-- menu 1 now exists
-- the pop up button is open
-- do what must be done
end if
end tell
end tell
on error errstr
display alert "Something went wrong" message "The script sent this error: " & errstr
end try
end tell
return 0.2
end idle
You can leave that running in the background watching for specific changes in the GUI (the 'try' statement is in case the app you're watching quits, the window closes, or something unexpected happens to the GUI).
If you haven't already, open the System Events scripting definition in Script Editor and look at the Processes Suite. That will show you all the things you can do with GUI scripting.

Applescript - Idein

I'm newbie to Appplescript. I need to automate certain actions on my computer related with my Bluetooth keyboards.
I want to be able to click on the remove or connect button of a keyboard in the following dialog window of the System Preferences Panel.
Dialog window
My code until this moment is as follows:
try
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.keyboard"
end tell
tell application "System Events"
tell process "System Preferences"
click button "Set Up Bluetooth Keyboard…" of window "Keyboard"
end tell
end tell
tell application "System Events"
tell group 1 of window 1 of application process "System Preferences"
click button "remove" of "Home Keyboard"
end tell
end tell
end try
My problem is related with the remove button since is unidentified cell of an unidentified table. With unidentified, I mean without description. Maybe there is an easy solution, but I'm not able to find it. Furthermore, It could happen that more than one keyboard exists, so I need to identify the cell from the Keyboard name.
Do you know any hint related with this issue?
Thanks in advance
Here is a sample script I used to reconnect a specific mouse via Bluetooth :
tell application "System Events"
tell application "System Preferences"
activate
reveal anchor "MouseTab" of pane id "com.apple.preference.mouse"
end tell
tell application process "System Preferences"
click button "Configuration of Bluetooth mouse…" of window 1 -- see note 1
delay 1
select (first row of table 1 of scroll area 1 of sheet 1 of front window whose value of item 1 of static text of UI element 1 contains "Mouse") -- see note 1
get value of item 1 of static text of UI element 1 of row 2 of table 1 of scroll area 1 of sheet 1 of front window
click button "Done" of sheet 1 of front window -- see note 1
end tell
tell application "System Preferences" to quit
end tell
Note 1 : Be careful about the 3 lines with comment 'see note 1' : the value of the string may be different for your local language. Please adjust these 3 values.
I think for keyboard, concept should be very similar. Because it is using GUI scripting, if Apple changes the layout of Bluetooth screen preferences, it must be adjusted. This script works from Yosemite to ElCaptain.I can't test it for next systems.

Applescript play music from iTunes URL

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

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.

Set focus to specific window of an application using applescript

How can I set focus to a specific window of a given application using applescript?
I have several iTerm2 windows running on different displays. I want to set focus to a specified window using applescript.
I need two things, one script that collects the window ID's and prints them to stdout. I've got this:
tell application "iTerm"
set wins to id of every window
end tell
which prints 6 integers: 3034, 2528, -1, -1, -1, -1
Bonus Question: What are the four -1's ?
Then I try:
tell application "System Events"
activate window 3034
end tell
Upon which the only thing happening is that I lose focus of my current terminal (in which I am typing these commands), not matter whether I specify 3034 or 2528 as the ID.
You almost have it. You can filter out the "-1" window IDs as by only looking at visible windows:
tell application "iTerm 2"
set wins to id of every window whose visible is true
end tell
I figured this out by looking at the results of:
tell application "iTerm 2" to properties of every window
I noticed that the "-1" windows have the property visible:false
Then you can tell the window ID directly to the iTerm application instead of system events:
tell application "iTerm 2"
activate window 13195
end tell

Resources