How can I open the Dropbox preferences window using Applescript? - macos

How can I open the Dropbox preferences window using Applescript?
I'm assuming it would be something along the lines of
tell application "System Events"
tell application "Dropbox"
keystroke "," using command down
end tell
end tell
But this isn't working. Perhaps it's because Dropbox is running in the tray / background? ie: I can't switch to it using command-tab.
Thanks!

Unfortunately there is no way to do this. NSStatusItems (of which Dropbox is one) are not visible via accessibility. This weblog entry and this bug provide more information. Please file a bug and reference that bug number if this is important to you.

Related

Automator permissions error when running AppleScript in Safari – Alternatives and security risks?

I want to use Automator and AppleScript to fill out a form in Safari. I have a functioning script that looks something like this toy example:
on run {}
tell application "System Events" to keystroke "Hello"
tell application "System Events" to keystroke tab
tell application "System Events" to keystroke "World"
end run
I wish to execute it with a keyboard shortcut when I'm at a specific point in my browser. However, when I do this, I run into permission issues:
The action "Run AppleSCript" encountered an error:
"System Events got an error: Automator Workflow Runner
(WorkflowServiceRunner, my_script_name) is not allowed to send keystrokes"
In System Prefrences -> Security & Privacy -> Privacy -> Accessibility I have allowed both Automator and AppleScript Utility, and under the Automation tab, I have allowed System Events for Safari.
I can get it to work, at least temporary, if I allow Safari in Accessibility too, but that seems to be too drastic and a security risk to have Safari to always have full control just to run a script from time to time.
How should I approach this? I want the script to be easy to use, and wouldn't want to go into the settings each time to temporary allow Safari. Or can this be automated too? Is there a completely different approach than Automator and AppleScript that would allow me to do this in a more user friendly and safe way? Basically, I want to do something similar to what AutoHotKey would be able to do in Windows.
I’m guessing that what you’ve done is set up a Quick Action and then given that Quick Action a shortcut under “Services” in “Keyboard:Shortcuts” in System Preferences. I was able to duplicate the problem by doing that: attempting to run the script requires that Safari itself have access to “control your computer”. If that is not what you’ve done, this solution should still work, but you may also want to update your question, in case a better solution is available.
What you can do is use redirection. Create an application that can be given permission to control your computer, and then create a Service that will launch the application.
Use Automator (or Script Editor) to create an Application with the above steps, adding as the first step tell application "Safari" to activate. You need that step because opening the app will take the focus away from Safari.
Save this Application. (If using Automator, you tell Automator that it’s an application when you start creating the script; if using Script Editor, you tell Script Editor that it’s an application when you save the script.)
Use Automator to create a Quick Action. Give it the action “Launch Application” and tell it to launch the application you just created.
Drag the application you created in step 2 into the “Accessibility” list in System Preferences’s “Security & Privacy” settings. (You may have to unlock those settings to allow changes.) It should be checked automatically when you drag it in, but if not you can check it.
In the “Services” list in the “Keyboard:Shortcuts” settings of System Preferences, the Quick Action you created in step 3 should be listed. Give it a keyboard shortcut.
At this point, you should be able to use the keyboard shortcut to run the application, and only that application needs to have permission to control your computer.

AppleScript to Launch 1Password mini

I use 1Password to store my passwords, and fluid.app to create a few site-specific apps/browsers
While 1Password integrates well with Chrome, Safari and other browsers, it doesn't play nicely with Fluid apps, which is where my troubles lie.
I have therefore created a shortcut (with Keyboard Maestro) to run an applescript to open the mini, when I am in Fluid apps. However, I can't seem to get the applescript to launch the mini. Here are my attempts:
tell application "1Password mini" to launch does nothing, though AppleScriptEditor doesn't report any errors
tell application "1Password mini" to open nothing, though AppleScriptEditor doesn't report any errors
tell application "1Password mini" to activate is pointless, as 1Password mini is already activated
tell application "1Password" to open opens the main 1Password application, not the mini, which is what I need.
Interestingly, when I run (1), (2), and (3), AppleScriptEditor automatically replaces 1Password mini with 2BUA8C4S2C.com.agilebits.onepassword-osx-helper, which doesn't clarify things for me at all.
Does anybody have any clue how this can be solved?
I'm on OSX 10.10.2 (Yosemite), if it matters
I work for AgileBits, the developer of 1Password.
Looks like you found a solution, but here's another option for you:
open location "x-onepassword-helper://search/"
That AppleScript will open 1Password mini ready to search.
And if you're able to get the current website's domain from Fluid, you could even open 1Password Mini with a search term already filled in:
open location "x-onepassword-helper://search/twitter.com"
I hope that helps!
The solution was quite simple, with a little UI scripting:
tell application "System Events" to tell process "1Password mini"
tell menu bar item 1 of menu bar 1 to click
end tell

AppleScript and non-scriptable applications

I'm wondering how can I script applications which has no dictionary. All information that I've found tells me nothing. But my experience says me that there is a way.
For example:
tell application "Firefox"
return count of windows
end tell
Will work. And it will work with "Opera" and other applications without dictionary at all. So the questions are:
1) Why does it work?
2) What else work in such a manner? Is there a list of all such actions?
Thank in advance!
You can to some extent script applications without an applescript dictionary by using 'tell application "System Events"'
tell application "Keynote" activate
tell application "System Events" to keystroke "c" using {command down}
end tell
end tell
This example activates Keynote and then copies the current selection. You can use similar code in many applications even if they don't have an applescript dictionary provided you also have the "Enable access for assistive devices" option checked in the "Universal Access" System Preference.
Edit:
This document gives some details of how Cocoa provides some support for Applscript in all applications:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ScriptableCocoaApplications/SApps_about_apps/SAppsAboutApps.html
Under the heading Built-in Support for Standard and Text Suites
You can open the scripting dictionary of FireFox. Just open AppleScript Editor and go File → show dictionary, and choose FireFox from the list.
It just shows a very rudimentary dictionary. What happens is that the system library provides at least a basic set of commands (called the Standard Suite) to any Carbon or Cocoa app. This is what contains the definitions of window you used.
As Ian already wrote, in order to do more with such an app, you use the so-called UI scripting via System Events.
This nice website is a good place to start.

MacOSX or AppleScript: get current URL from Firefox

How can I get the current URL from Firefox (3 or 4)? All solutions I have found so far either don't work (e.g. those with "class curl") or are ugly hacks which are no solution for me (sending key presses to copy the URL to clipboard).
Well, I have one solution now: I am reading it out of the current session store of Firefox. This works but it is always up to 10 seconds out-of-date.
Here is the code:
https://github.com/albertz/foreground_app_info/blob/master/mac/app-scripts/url%20of%20Firefox.py
tell application "Firefox" to activate
tell application "System Events"
keystroke "l" using {command down}
keystroke "c" using {command down}
end tell
Easy, if you install two components:
the mozrepl firefox extension (github)
and WWW::Mechanize::Firefox perl module
If you have installed both, the following command
perl -MWWW::Mechanize::Firefox -E 'say WWW::Mechanize::Firefox->new(tab=>'current')->base()'
will print the topmost active firefox url, in like:
http://stackoverflow.com/questions/5296995/macosx-or-applescript-get-current-url-from-firefox
Since Firefox 87 you can use native AppleScript GUI scripting to get the current URL. That's because Firefox now has support for VoiceOver.
First enable Firefox support for VoiceOver by going to about:config and setting the accessibility.force_disabled property to -1. Note that VoiceOver doesn't have to be enabled, only the support in Firefox. (Extra info at [2].)
After that, you can use:
tell application "System Events" to get value of UI element 1 of combo box 1 of toolbar "Navigation" of first group of front window of application process "Firefox"
[1]: Enabling VoiceOver support makes Firefox expose the internal structure of its window for GUI scripting.
[2]: For extra info and a non-permanent option (toggling AXEnhancedUserInterface via AppleScript) look at this bug report.
A window's URL is not exposed via the Applescript API; not even a window's tabs. The only way you'll be able to get it is through GUI scripting.
Firefox's Applescript implementation is simply awful, and I don't know why they even bother.
Firefox 3's AppleScript implementation is lame enough that you have to resort to copying the URL out of the Location field, as shown in the last post is this thread.

Code for minimizing application window in Mac?

I'm wondering whether there is a way to write code for Mac OS 10.5 which will minimize and restore a window. What language would it be in? Could someone please give me an example or direct me to documentation on Apple's developer site I should look at?
Thanks!
Try this applescript:
tell application "Safari"
set miniaturized of window 1 to true
end tell
Apple | AppleScript
AppleScript is Apples native scripting language. The official link above includes very useful information on how you can get started.
I suspect that the following URL is what you may need. It provides information and sample code for scripting and automation in AppleScript:
AppleScript | Scripting and Automation
I had trouble with these working in 2023.
This worked for me to open chrome and then minimize.
tell application "Google Chrome"
activate
delay 1
end tell
tell application "System Events" to set visible of process "Google Chrome" to false

Resources