Applescript to de-authorize a computer for iTunes - applescript

I have a computer (Mac) that is authorized to play music through iTunes which I want to disable. But, I only have ssh access to the machine. Is there an applescript I can run remotely (eg via the terminal) that I can use to de-authorize the machine?
I know that I can unauthorize all the machines I have authorized, but I would prefer to use this solution if it is possible.

I don't see any properties to authorize/deauthorize in iTunes dictionary but I just played around with GUI scripting and came up with a solution. So, the target Mac will have to have GUI Scripting enabled for the script below to work.
tell application "System Events"
tell process "iTunes"
click menu item "Deauthorize This Computer…" of menu 1 of menu bar item "Store" of menu bar 1
delay 1
set frontmost to true
click menu 1 of menu bar item "Store" of menu bar 1
set value of text field 1 of window "Deauthorize This Computer" to "password"
click button "Deauthorize" of window "Deauthorize This Computer"
end tell
end tell
You could leave that AppleScipt on your target Mac and then just use the open command to launch it. Or you could copy the above AppleScript and paste it into a shell script and use the HEREDOC method with osascript.
The full example of this looks like this:
osascript<<END
tell application "System Events"
tell process "iTunes"
click menu item "Deauthorize This Computer…" of menu 1 of menu bar item "Store" of menu bar 1
delay 1
set frontmost to true
click menu 1 of menu bar item "Store" of menu bar 1
set value of text field 1 of window "Deauthorize This Computer" to "password"
click button "Deauthorize" of window "Deauthorize This Computer"
end tell
end tell
END
The above method works well with Apple Remote Desktop's Send Unix feature as well.
Also, note that the password is included in this script which I don't recommend
but it is needed on the deauthorize window. If you put the password in the script make sure to secure that script so no one gets ahold of your Apple password.

Related

How to send a menubar app (with no menubar menu items) a command in AppleScript?

I would like to control an application (Shady.app) with an AppleScript command. Unfortunately it doesn't have the standard menubar items such as File, Edit, View, etc.
My goal is to somehow issue a command to the app through AppleScript such that it will toggle the "Turn Shady On" & "Turn Shady Off" command:
NOTE: "Turn Shady On" is the OFF state and it turns to "Turn Shady Off" when Shady is in the ON state.
How can you create a script that toggles this both directions based on it's current state in AppleScript? Or is there a better way than AppleScript to control this programmatically?
This AppleScript will "Turn Shady Off"
tell application "System Events"
try
click menu item "Turn Shady Off" of menu 1 of menu bar item "Shade" of menu bar 1 of application process "Shady"
click menu bar item 1 of menu bar 2 of application process "Shady"
end try
end tell
This AppleScript will "Turn Shady On"
tell application "System Events"
try
click menu item "Turn Shady On" of menu 1 of menu bar item "Shade" of menu bar 1 of application process "Shady"
click menu bar item 1 of menu bar 2 of application process "Shady"
end try
end tell
Assuming it's being run in the status bar.
This will toggle on and off
on toggle()
tell application "System Events"
try
click menu item "Turn Shady On" of menu 1 of menu bar item "Shade" of menu bar 1 of application process "Shady"
end try
try
click menu item "Turn Shady off" of menu 1 of menu bar item "Shade" of menu bar 1 of application process "Shady"
end try
click menu bar item 1 of menu bar 2 of application process "Shady"
end tell
end toggle
toggle()
Save it as an application In ScriptEditor or as a service in Automator or whatever you choose. No need for FastScripts.
The standard way to send a command via short-cut is by:
System Preferences > Keyboard > Shortcuts > App Shortcuts
Click the + and select Shady, then you'll be able to define the short-cut you want to use. Keep in mind you'll need to use ⌘ + Tab to switch to Shady first, then use the short-cut.
If you use an application like FastScripts then you can create a global short-cut without having to switch to the application first which is sometimes better if the app doesn't have focus.
You can do something like this via Keyboard Maestro. Here's an image demonstrating what you want in action:
For the record, I also thought this was so awesome that I also included a way to create a darken/lighten global shortcut:
You've also gotta have "Show Dock Icon" turned on in system preferences for this to work!
See this post on the forums for more info. I'll go ahead and paste it here as well for convenience's sake and to circumvent link rot:
sonicly:
I have an application that runs only as a menubar app. It has a simple command that is essentially "Turn App On"/"Turn App Off". Is there a way to make Keyboard Maestro set up a global hotkey to initiate the command in this app?
I just ran across this topic which addresses your issue: osx - How to send a menubar app (with no menubar menu items) a command in AppleScript? - Stack Overflow
I don't know if you are the OP or not, and I have NOT tested the suggested solution. But maybe it will at least get you started.
sonicly:
So I can enable it to show up on the Dock and with that I was able to set keyboard shortcuts that work... But I have to get focus on the app first (i.e., cmd-tab). That's a hassle and I'd rather just issue something like command-option-shift-space to turn it on and then to toggle it back off again. That seems doable with KM, but I'm not sure how to make the Maestro do that per se
This is very easy in KM, just use the
Activate a Specific Application action (KM Wiki)
Type a Keystroke action (KM Wiki) to issue the shortcut key.
Another option is to use the Select or Show a Menu Item action (KM Wiki), but I'm not sure if it will work in this situation.

AppleScript: Menu bar item click (but no AXDescription)

I'd like to AppleScript to click a menu bar icon (not a system process) and then click on one of the menu items. I'm using OS X 10.10.
I've read that I can use the Accessibility Inspector to find the AXDescription of the item, however, I can't find it anywhere in the inspector (in fact none of the items I inspected seemed to have this property).
I also tried the method described here but get an error on line 10:
ignoring application responses
tell application "System Events" to tell process "Webcam Settings"
click menu bar item 1 of menu bar 2
end tell
end ignoring
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "Webcam Settings"
tell menu bar item 1 of menu bar 2
click menu item "Show Webcam Settings Panel" of menu 1
end tell
end tell
System Events got an error: Can’t get menu 1 of menu bar item 1 of menu bar 2 of process "Webcam Settings". Invalid index.
There are some menu bar apps which you can click and show menu with applescript regardless of if they appear in dock.
But I am afraid you can’t access the menubar of “Webcam Settings” with applescript.
Because there isn’t key named “isAccessbiliyFocused” in the attributes.
Menu bar apps you can show their menu with applescript have that key and its value is “Yes”.
I am not quite sure about my answer but as far as I investigate this issue, my answer is probably right.

Access context menu items by name using AppleScript

I'm trying to use AppleScript to click on context menu items in Logic Pro, preferably by simply providing the name of the menu item. It seems like this should be possible because I'm able to set up keyboard shortcuts for these context menu items using system preferences and providing the name of the command.
For instance, if you right click on the main editing window in Logic, a menu pops up with an option called "Add Audio File..." If I create a system preferences keyboard shortcut for Logic and give it this menu item name, it's able to execute just fine. I'd like to recreate this with a script. I'm familiar with accessing normal menu items using the hierarchy like so:
tell process "Logic Pro"
tell menu bar 1
tell menu bar item "File"
tell menu "File"
click menu item "Save"
but as far as I know, there's no way to access the context menu (right click menu) that I want like this. It seems there should be a way to simply access a non-menu-bar menu item by name since system preferences is obviously able to do so.
Logic pro is not scriptable so my suggestion would be that you set a keyboard shortcut in the system preferences then use system events to use said shortcuts.
for example to enter find mode (assuming there is a find mode since i don't own Logic Pro)
tell application "Logic Pro" to activate
tell application "System Events"
tell application process "Logic Pro"
keystroke "f" using command down
end tell
end tell
I don’t think you need to use the context menus. “Add Audio File…” is available in other parts of the Logic Pro X user interface. If you open the Project Audio window, there is an “Audio File” menu button with an “Add Audio File…” button in it. So this AppleScript will activate the “Add Audio File…” command:
tell application "System Events"
tell application "Logic Pro X" to activate
tell process "Logic Pro X"
tell menu bar 1
click menu item "Open Project Audio" of menu "Window"
end tell
delay 1
tell window 1
click menu button "Audio File" of group 1 of group 1
click menu item "Add Audio File..." of menu 1 of menu button "Audio File" of group 1 of group 1
end tell
end tell
end tell
One thing to keep in mind if distributing a GUI script is that the above script will only work in Logic Pro X running on a Mac set to US English (and maybe other kinds of English) because the names of the menus change if the system is set to another language. What you can do is replace the names in the above script with numbers, which is a totally experimental process, as far as I know. You have to try different numbers and see which continues to work.
So you may be able to replace:
menu button "Audio File" of group 1 of group 1
… in the above script with:
menu button 1 of group 1 of group 1
… and get the same functionality, and the script would work on any Mac. Or you may need to use “menu button 2.” Same goes for the other named items in the above script.
Also keep in mind that the user you distribute this script to has to give System Events permission to control their Mac in the Security pane of System Preferences or this script won’t work. That can be a giant obstacle to distributing GUI scripts. And if you save your script as an Application, you have to digitally sign it or it won’t run on other people’s computers, and that can be complicated.

Applescript to click on a specific icon in the Mac Menu Bar

Sometimes I use PdaNet to tether using my iPhone. The desktop client for OSX is not as rich as the one for windows. One of the chief differences is, that the OSX does not allow to automatically connect to iPhone as soon as the latter is plugged in.
Would you know of a way using Applescript to click on the PdaNet icon on the Menu Bar and then select and click the 'Connect' option on it ?
Here is what the 'PdaNetMac' application's menu bar icon looks like:
I have looked at the following questions but am an applescript newbie and am not sure how to search for PdaNet's icon on the menu bar:
Click menu item on Mac OSX Lion using AppleScript
Applescript: on clicking Menu Bar item via gui script
Accessing dock icon right-click menu items with AppleScript
I have confirmed that 'Enable Access for assistive devices' is enabled.
Based on the second question above, Here is my current attempt at doing this:
ignoring application responses
tell application "System Events" to tell process "PdaNet"
click menu bar item 1 of menu bar 2
end tell
end ignoring
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "PdaNet"
tell menu bar item 1 of menu bar 2
click menu item "Connect" of menu 1
end tell
end tell
Interestingly, the above script works for me fine when I change PdaNet to Flux.
Thanks!!
You were very close !!
I just downloaded the PdaNet application to test this, and the only edit I had to make to your script was change PdaNet to 'PdaNetMac` ( I was thinking that this is the Process Name and so used the process name displayed in Activity Monitor).
So this works for me:
ignoring application responses
tell application "System Events" to tell process "PdaNetMac"
click menu bar item 1 of menu bar 2
end tell
end ignoring
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "PdaNetMac"
tell menu bar item 1 of menu bar 2
click menu item "Connect" of menu 1
end tell
end tell
Hope this works for you too !!
(Very useful script, btw. Cheers !)

Using AppleScript to click the plus (+) button in a preferences window

Using AppleScript I would like to automate the creation of a new user accounts on multiple machines. I'm trying to use UI Scripting and cannot seem to be able to do a click command on the "Add Account" (+) button in the iChat Accounts Preference window. I've tried to use the Accessibility Inspector.app to infer the name of the button but I still cannot seem to access it via AppleScript.
According to Accessibility Inspector.app the button has the following attributes:
AXRole: AXButton
AXRoleDescription: button
AXHelp: <nil>
AXEnabled: YES
AXFocused (W): NO
AXParent: <AXGroup>
AXWindow: <AXWindow:AXStandardWindow>
AXTopLevelUIElement: <AXWindow:AXStandardWindow>
AXPosition: x=225.00 y=462.00
AXSize: w=23.00 h=22.00
AXTitle: <empty string>
AXIdentifier: _NS:27
AXDescription: Add Account
Here is a sample starter AppleScript that will get you to the Accounts Preference window:
tell application "iChat"
activate
tell application "System Events"
tell process "iChat"
tell menu bar 1
click menu item "Preferences…" of menu "iChat"
end tell
click button "Accounts" of tool bar 1 of window 1
click button "Add Account" of window "Accounts"
end tell
end tell
end tell
Please note that line 9 is what I'm trying to get working. Below you will find a link to a screenshot of the iChat Accounts Preference window with an arrow pointing to the button that I'm trying to access. Also note that I know that I can just use "keystroke tab" to get to the (+) button, but this will not always work as the number of times you must press tab changes depending on the type of accounts that you already created in iChat.
iChat Account Preferences Window
Not every Ui object has a title, so in those cases you can just use the item number - note that you need to include the entire hierarchy shown in the Inspector window.
tell application "iChat"
activate
tell application "System Events"
tell process "iChat"
tell menu bar 1 to click menu item "Preferences…" of menu "iChat"
delay 0.5
tell window 1
click button "Accounts" of tool bar 1
delay 0.5
click button 1 of group 1 of group 1
end tell
end tell
end tell
end tell

Resources