Mac OSX - Firefox --new-tab option doesn't work - macos

I was trying to force a new tab to open in firefox via a terminal command.
If I run /Applications/Firefox.app/Contents/MacOS/firefox --help, it tells me that there is a --new-tab <url> parameter.
But when I make use of it, firefox reports:
Close Firefox
A copy of Firefox is already open. Only one copy of Firefox can be open at a time.
Googling online revealed reasons and a potential workaround:
https://support.mozilla.org/en-US/questions/1130718
https://bugzilla.mozilla.org/show_bug.cgi?id=393645
I tried the workaround AppleScript, but it only worked on the first run attempt, and failed on every other occasion.

I mainly posted the question as a means to document and share my solution.
I made use of what I believe to be Ivan Enderlin's Apple Script, "ffnewtab.scpt". The only addition I made to it was to add a one second delay (the "delay 1" line) between activating firefox and creating a new tab. It seemed as though on my system, the script ran too fast and didn't give firefox enough time for the new tab to open, probably due to my macbook being somewhat old and slow:
on firefoxRunning()
tell application "System Events" to (name of processes) contains "Firefox"
end firefoxRunning
on run argv
if (firefoxRunning() = false) then
do shell script "open -a Firefox " & (item 1 of argv)
else
tell application "Firefox" to activate
delay 1
tell application "System Events"
keystroke "t" using {command down}
keystroke item 1 of argv & return
end tell
end if
end run
Then it runs fine for me with:
osascript ffnewtab.scpt <url>

Related

Alfred Workflow - Searching on specified web browser in a new window with optional argument

I trying to make a workflow of my taste, but I'm having a real hard time. I'm new to programming in general, but I'm trying my best so any help would be appreciated!
Here's my Goal: I want to create a workflow that is triggered by a key word, with optional arguments, so that when there are no argument, a specific (not the default) browser to open a specific (not the homepage) URL, in a new window regardless the web browser app is running or not. When provided with an argument, I want to search for the argument. So for example, if I type "yy" , then Chrome opens Youtube in a new window, and if I type "yy alfred" then Chrome does a search on alfred on Youtube.
The first issue I ran into was that when apps are not running in the background and are activated, if I activate and specify an URL, I get 2 new windows. I looked it up and I did fix it (I think).
The real problem is that I cannot find how to retrieve? import? the argument as {query} and search for the argument. I've tried what the default web search does on alfred, but no luck at all. Here's my script :
on alfred_script(q)
if q is equal to "" then
if application "Google Chrome" is running then
run script "tell application \"Google Chrome\"
activate
open location \"https://www.youtube.com\"
end tell"
else
set theApp to quoted form of "Google Chrome"
do shell script "open -n -a " &theApp & "
'https://www.youtube.com'"
end if
else
set query to q
if application "Google Chrome" is running then
run script "tell application \"Google Chrome\"
activate
open location \"https://www.youtube.com/search?q={query}\"
end tell"
else
set theApp to quoted form of "Google Chrome"
do shell script "open -n -a " &theApp & " 'https://youtube.com/search?q={query}'"
end if
end if
end alfred_script
Any ideas on how I can improve my script? Thanks!

Why this bug - applescript , automator & applescript editor

My applescript gave correct output when ran inside automator as service with no input but it gives error message when ran outside automator (safari, preview etc.)
It also gives correct output when ran through applescript editor, no problem running it with safari, preview or any other application.
The reason I was so keen to get it done with automator was that I don't want to install a 3rd party app just for assigning one shortcut to one script.
I debugged this portion which after removing from code allowed automator to run the script. Ran it with try, error in automator & viola, no issues at all.
But I want to know why this code earlier & without try-error function was giving the error messages when ran outside automator application.
Here's the portion of the script with try-error function:
try
set writ to do shell script "/usr/bin/python Users/[username]/Documents/tech_toolkit/windtitle"
tell application writ
if the (count of windows) is not 0 then
set window_title to name of front window
end if
end tell
on error
delay 2
end try
For shell script /windtitle , windtitle is an executable file
#!/usr/bin/env python
from AppKit import NSWorkspace
active_app_name = NSWorkspace.sharedWorkspace().frontmostApplication().localizedName()
print active_app_name
The error is:
Your python script return the "Automator Runner" application when it run as service , it's
the frontmost in the background but not in the foreground.
To get the frontmost application, use this script:
try
tell application (path to frontmost application as text)
set window_title to name of front window
set writ to its name -- get the name of this application
end tell
on error
return -- quit this script, because this application is not scriptable, or no window
end try

Run a script in an existing Terminal window when the current line already contains some text

If the current line in the frontmost Terminal tab contained the text aa, this would run the command aauptime:
set cmd to "uptime"
tell application "Terminal"
try
do script cmd in window 1
on error
do script cmd
end try
activate
end tell
You could obviously add something like tell app "System Events" to keystroke "ku" using control down before do script, but does anyone know any better solutions?
You know you can call a shell script without targeting the terminal, right?

An applescript stopped working after upgrade from snow leopard to lion

This script worked every time with Snow Leopard.
tell application "Terminal"
activate
do script "cd web_sites/project" in front window # this line highlighted on error
do script "mate ." in front window
do script "rvm 1.8.7" in front window
do script "script/server" in front window
delay 4
do shell script "open -a Firefox http://localhost:3000"
end tell
With Lion I keep getting this error:
error "Terminal got an error: Can’t get window 1." number -1728 from window 1
Thanks.
I'm running Lion. I can execute the following with no errors. I do not get an error in Applescript in any of the following situations 1) the application is not running, 2) the app is running and a window is open, and 3) the app is running and the directory path is not valid. In case 3 the Terminal shows an error but applescript does not.
tell application "Terminal"
activate
do script "cd Development/Images" in front window -- this line highlighted on error
do script "ls -al" in front window
end tell
So your problem is something not related to this actual code.
It seems the problem is not the code but the speed at which the operating system is functioning. As i mentioned before, the code executed just fine in Snow Leopard. To compensate, after the upgrade to Lion, if i add another delay to give Terminal time to finish activating, and increase the delay before opening Firefox, everything works. My laptop is a MacBook Pro with 2.7 GHz Intel Core i7 processor.
tell application "Terminal"
activate
delay 1
do script "cd web_sites/project" in front window # this line highlighted on error
do script "mate ." in front window
do script "rvm 1.8.7" in front window
do script "script/server" in front window
delay 5
do shell script "open -a Firefox http://localhost:3000"
end tell
If you are just trying to get a script to run once, simply retrying may help. Execution speed is also a function of other demands on the system.

Using AppleScript to choose a file in Safari

I am trying to write some automation code (primarily in Ruby Selenium). At some point, a file chooser is opened in Safari so that the user can select a file for upload. Selenium cannot handle this, but I think AppleScript should be able to. I am new to AppleScript and haven't been able to find any boilerplate code of someone automating a file chooser dialog. I'm reading through the AppleScript docs, but any ideas would be most helpful.
Some more searching and I found a great answer here: Applescript file dialog with UI scripting
Here's what I ended up using:
on run argv
tell application "Safari"
activate
-- Usage check
set argc to count argv
if argc is not greater than 0 then
return "Usage: SafariFileChooser file_name [window_name]"
end if
-- The file we will choose to open
set file_name to item 1 of argv
-- Flip to the named window, if specified
if argc is equal to 2 then
set window_name to item 2 of argv
set flip_count to index of window window_name
repeat (flip_count - 1) times
activate
tell application "System Events" to keystroke "`" using command down
end repeat
end if
-- Interact with the dialog using System Events (thanks mcgrailm)
tell front window
activate
tell application "System Events"
keystroke "g" using {shift down, command down}
keystroke file_name
delay 1
keystroke return
delay 1
keystroke return
end tell
end tell
end tell
return 0
end run
Another option I just discovered is to specify the directory using the command-line:
do shell script "defaults write com.apple.Safari NSNavLastRootDirectory /path/to/directory"
This way you can do slightly less in UI scripting. Run this command before you open the file chooser, and it will put you into the directory specified. Include all the files you need in this directory, and you can just script command+a to select them all, and return.

Resources