I'm new to use Applescript to create services in Snow Leopard. I found myself often trying to open a link in Safari with Firefox. I know there are ways to open a page url with FF but I want to open any link inside a page with FF. I think using Applescript to create a service might be a good idea and so far I found this:
openFirefoxURL("http://www.apple.com/")
on openFirefoxURL(x)
return do shell script "open -a Firefox" & space & quoted form of x
end openFirefoxURL
This creates a new tab in FF nicely. Any advice to adapt it to open a link on a page?
UPDATE: I found a solution through trial and error:
tell application "Safari"
set myURL to (do JavaScript "(getSelection().anchorNode.parentNode.href)" in document 1)
end tell
do shell script "open -a Firefox" & space & myURL
Now that the script serves my purpose, I don't know if there are better ways to do it, e.g. getting the selection via Applescript rather than javascript. Let me know if you have better solutions. Thanks!
There is another approach I just wrote after searching the web for a while an not finding a suitable solution.
It is a service in AppleScript that will open any URL in Firefox from any other application URL field. Just select the text in the URL of Chrome, for instance, and choose the service from the list.
To create the service:
Choose service in the main Automator dialog.
Choose "service receives selected" [URLs]
Choose "in" [any application]
Choose "input is" [only URLs]
Then add the action "Run Shell Script" dragging it to the script sequence.
Choose "Shell" [/bin/bash]
Choose "Pass input" [as arguments]
And paste the following script:
for f in "$#"
do
echo "$f"
if [ ${f:0:4} = "http" ]; then
open -a Firefox "$f"
else
prefix="http://"
prefix+=$f
open -a Firefox "$prefix"
fi
done
The script checks if the "http" prefix is added, as Chrome does not pass it by default, and opens Firefox with the OSX 'open' command.
Save this Service with the name "Open URL in Firefox" and you are done. The service will be available at the services list menu after a right click over any URL field selection.
Done.
The service is saved at ~/Library/Services. This directory is hidden by default in Lion, to see it just issue this command at terminal:
# chflags nohidden ~/Library/
You don't need a special program to do this. Safari already does it. You have to first enable the "Develop" menu in Safari. Go to Safari's preferences, go to the advanced section, and check the box at the bottom called "Show Develop menu in menu bar". Now that you have the Develop menu, you can open any page from Safari in another browser by going to Develop --> Open Page With menu.
So doing that will open the current page in Firefox, then just click on whatever link you want in Firefox.
I'm not sure if I entirely understand what you are trying to do, but if you wish to use Firefox as your default browser, then you don't need Applescript. Just go into Safari, open Preferences, then select Firefox as the default browser at the top of the first tab.
As Safari’s AppleScript dictionary does not provide any way to access the DOM, the JavaScript solution is a pretty good one. If it annoys you to have a service (“Show URL in Firefox”) available even when there is no link in the selected text, I’d recommend Choosy, a helper application / Preference pane by George Brocklehurst which will give you a user prompt for browsers (and much more, up to pattern based selection rules). There is a companion Safari extension which provides the contextual menu option you are looking for.
Related
I want to make an AppleScript to, if the first part of the link is ____ it will open it in Chrome.
open http://stackoverflow.com -a "Google Chrome"
I got that, but how would I do it depending on the first part of the link.
For example, if the first part of it is https://meet.google.com/xxx-xxxx-xxx to open in Google Chrome.
The xxxxx changes every time.
Alternative solutions welcomed too.
Here is an alternative solution, tested under macOS High Sierra 10.13.6 and it worked:
Automator > New > Service
With settings: Service receives selected text in Calendar
Add a Run Shell Script action, with settings:
Shell: /bin/bash
Pass input: as arguments
Replace the default code with: open "$1" -a "Google Chrome"
Save it as: Open URL in Google Chrome
Then in Calendar, right click on the URL and select Open URL in Google Chrome from the Services context menu.
It's not a single click scenario, but gets the job done.
Without knowing the value for xxx-xxxx-xxx, It's difficult to give an exact answer. I think if you look at the variables I set, This code should give you a good starting point to work with
set baseURL to "https://meet.google.com/"
set openURL to "https://meet.google.com/xxx-xxxx-xxx"
if openURL contains baseURL then do shell script "open " & openURL & " -a 'Google Chrome'"
I want to modify/change/add MacOS Dock shortcuts/hotkeys.
e.g., of a shortcut that is available by default:
Option-Click on Dock app icon of an app that is not currently open = Hide the currently active app and then Open the app that was clicked
(from: https://support.apple.com/kb/PH21922?locale=en_US)
One very specific ability that I want:
Open and then Hide an app
Shift-Click on Dock app icon = Open and then Hide that app
(or use another easy modifier-key-combo with the click)
I am aware of the bash command open -a App --hide (e.g., open -a TextEdit --hide). I want to implement this exact functionality with a convenient Dock shortcut like the one mentioned above. If you're wondering "why?": sometimes I just want to open an app because I know that I will need it soon, but I'm still busy with another app, so just open this second app and then immediately hide it so it doesn't get in my way while I'm still busy with that first app.
How do I do this?
You can't.
These keyboard bindings are built into the Dock application, and cannot be modified.
The Mac utility program, Keyboard Maestro, does what you've asked, with shortcut keys, without using the Dock:
open, then immediately hide/minimize
option+open
I came up with a related solution: (in case anyone is interested)
an AppleScript App that presents a pick list
Method:
create a plain text document containing a list of the apps you want to handle (use correct name, no path, no extension, one name per line, no commas)
open 'Script Editor' (/Applications/Utilities)
copy-pasta the following code (and edit the first code line for the path to your text file from first step)
set apps_file to ("path:apps_list.txt")
set apps_list to paragraphs of (read file apps_file)
set apps_pick to choose from list apps_list with prompt "Select one or more apps." with multiple selections allowed
if result is false then return
set path_base to "Macintosh HD:Applications:"
set path_msft to path_base & "Microsoft Office 2011:"
set path_utly to path_base & "Utilities:"
set spec_msft to "Microsoft"
set spec_utly to "Activity Monitor, Terminal"
repeat with apps_this in apps_pick
if apps_this contains spec_msft
set path_this to path_msft
else if apps_this is in spec_utly
set path_this to path_utly
else
set path_this to path_base
end if
set apps_open to path_this & apps_this & ".app"
run application apps_open
end repeat
return
i. navigate menu 'File -> Export...'; ii. use the option 'File Format: Application'; iii. Save; (put the resulting app in your Dock)
Its not quite as convenient as I hoped, but, not too shabby.
I managed to successfully silence the CrashReport dialog, but when my application crashes and I restart it, I get the annoying dialog as from Title. Is there a way to prevent it to appear, and just let the application run without interruption?
Try this to get rid of the reopening windows:
defaults write -app "Application Name" NSQuitAlwaysKeepsWindows -bool false
You may also disable it for every application by selecting this option in the preferences: "Close windows when quitting an application"
And for others reading this thread, to remove the CrashReport do this:
defaults write com.apple.CrashReporter DialogType none
Also note that in the source of this information they say:
For this to work one needs to check the box, open the program in
question and immediately close it. On the next re-opening it will work
without Resume.
You may also have to delete:
/Users/…/Library/Saved\ Application\ State/org.python.python.savedState/
I was having a similar problem with google chrome and I could solve it by reading the following link:
https://support.google.com/chrome/thread/22716083?hl=en
Drew Z recommends the following solution there which worked for me:
In the Mac menu bar at the top of the screen, click Go.
Select Go to Folder.
Enter ~/Library/Application Support/Google/Chrome/ in the text field, then press Go.
Locate the folder called "Default" in the directory window that opens and rename it as "Backup default."
Try opening Google Chrome again. A new "Default" folder is automatically created as you start using the browser.
Voila! I've just solved this problem by deleting all Unity-related files inside ~Library/Caches folder on my Mac!
For those trying to accomplish this
defaults write -app "Application Name" NSQuitAlwaysKeepsWindows -bool false
with Python, you may get the error Couldn't find an application named "Python"; defaults unchanged.
To solve this, repeat the process to get the "reopen windows?" pop-up again, but do not choose an option in the pop-up – leave it alone for now. Right-click on the Python application's icon on the dock and choose "Show in Finder". Right-click on the application icon within Finder, hold the option key, and click Copy "Python" as pathname". Paste that in as the "Application Name" for the command above and it should work.
You can disable this for a specific Xcode scheme by going to Edit Scheme, choosing the Options tab, and checking the box labeled "Launch application without state restoration."
However, this will only apply when you actually launch the application from Xcode; it won't disable the dialog when launching by double-clicking in Finder, or when launching from the terminal.
(As best I can tell, there no way for AppKit/NSApplication-based apps to do what UIKit apps can do with UIApplicationDelegate's application:shouldRestoreApplicationState: and disable persistent state entirely for the application.)
I create a game using html5 based on Chromium project (in Mac). In order to open the game in Chromium, select File menu and select the Open file and browse for the html. And the game is working fine this way
I want to write some script that opens the html in Chromium browser so that I can package it as an application and distribute it as DMG. Has any one come across Chromium script?
Try:
set myFile to "/Users/Satyam/Desktop/OriginalMsg.htm"
do shell script "open " & quoted form of myFile & " -a Chromium"
EDIT
set myFile to "/Users/Satyam/Desktop/OriginalMsg.htm"
do shell script "open " & quoted form of myFile & " -a Chromium"
tell application "Chromium"
activate
close (every tab of window 1 whose title = "New Tab")
end tell
I do not know how to open a url specifically in Chromium. However, normally you do it this way and it will open in a users default web browser. This is the normal way to open files, you don't target a specific application but you let it open in the users' preference.
Maybe it will help you. Good luck.
open location "http://www.url.com"
I know nothing about AppleScript, but I wonder if it could make my life easier: is there a way to write an AppleScript that tells Safari / Firefox / Chrome to refresh the current tab when I save a document in another application, say TextWrangler? Essentially, I want to map the Command+S keyboard shortcut to do two things at once in two separate applications.
If that’s not possible, can you script one application so that saving one file executes a command in another window in that same application?
There are different possible approaches to implement this, but the most straightforward would probably be to create a script that executes all steps you need (i.e. save the document and refresh the window) and bind that to the Cmd+S keyboard combo in the triggering application.
What you need for this approach to work, is, in order:
a method to bind a key combo to a script effective only in a specific application. OS X’ Automator Services fit that bill: their scope can be restricted to a single application (select it in the “only in” drop down at the top of the workflow actions), and they can be assigned a shortcut in the Keyboard preference pane of System Preferences.
a way to relay your commands to the applications they target. AppleScript can help you in two different ways, depending on the fact if your applications are scriptable, i.e. if they have a scripting dictionary you can inspect in the AppleScript editor:
if they do, and their terminology includes the saving action for the editor on one side (most scriptable document based apps do so in the form save <document>), the page refreshing for the browser(s) on the other (Chrome has reload <tab>, Safari gets the same result via a JavaScript detour, i.e. do JavaScript "window.location.reload()" in <document> – I don’t use Firefox), you are set.
if they do not, GUI Scripting might help, i.e. simulating a click on the right UI element (menu or toolbar) via tell application "System Events" to tell process <your process> to click item x of menu y.
That script can then be embedded into the Automator workflow (in an “Run AppleScript” action, to be precise).
As you can see, a lot depends on the exact software you are using. if you are new to AppleScript and the above baffles you, I’d suggest spending a bit of time on the AppleScript pages of Mac OS X Automation (where you’ll also find example scripts which will kick start you into things like GUI Scripting).
One final note: as of this writing, sandboxed applications do not honor key combos assigned to them in the Keyboard Preference pane (they do honor global key combos set there – just not those specifically targeting them). This means you cannot, for instance, currently override TextEdit’s Cmd+S shortcut for saving in Lion. As long as your editor is not sandboxed (easily checked in Activity Monitor), you should have no issue with this.
One solution would be to create a folder action to refresh the current tab when a new file is saved in the folder.
on adding folder items to theFolder after receiving theFiles
tell application "Google Chrome"
activate
tell window 1 to tell active tab
set URL to (get URL)
end tell
end tell
end adding folder items to