JvdL Posted January 24, 2018 Share Posted January 24, 2018 Hello, I searched this forum and also the web to find a workflow that can be triggered by a hotkey that opens a new Firefox window with several sites in several tabs opened at once. All the scripts or snippets I found where buggy or not working at all. What would be the right approach to do that. E.g. I would like to press a hotkey that opens: 1. A new Firefox window 2. First tab "www.nba.com" 3. Second tab "www.sbnation.com" I would appreciate your comments Jürgen Link to comment
JvdL Posted January 24, 2018 Author Share Posted January 24, 2018 What I have till now is this: delay 1 tell application "System Events" keystroke "n" using {shift down, command down} delay 5 keystroke "l" using {command down} keystroke "https://www.nba.com" & return delay 1 keystroke "t" using {command down} keystroke "l" using {command down} keystroke "https://www.wikipedia.org" & return delay 1 keystroke "t" using {command down} keystroke "l" using {command down} keystroke "https://www.cnn.com" & return delay 1 end tell end alfred_script But it does not work consistently . Sometimes it works as it should be, sometimes it just adds additional tabs to the Firefox Window in focus, sometimes it opens a new window, opening the previous set of tabs, ... Is this a problem with the script of with Firefox? Thanks Jürgen Link to comment
Vero Posted January 24, 2018 Share Posted January 24, 2018 Hi @JvdL and welcome to the forum There's a good built-in workflow you can use as example to teach you how to do what you describe; Open Alfred's preferences to the Workflows tab, and click the [+] button at the bottom of the sidebar. In Examples, choose "Should I watch this movie?" This workflow shows you how to use a keyword to launch three websites (YouTube, IMDB and Rotten Tomatoes). Now you can create your Blank Workflow to create your own in a similar way; Instead of a keyword, you'll start your workflow with a hotkey, and connect it to two "Open URL" objects. Set the URL for each site you listed above, and choose Firefox as the browser for each one. Let me know if you need any further help Cheers, Vero Link to comment
JvdL Posted January 24, 2018 Author Share Posted January 24, 2018 (edited) Thanks Vero, I will try that, too. I found a bug in my script and now it seems to work: on alfred_script(q) tell application "Firefox" to activate delay 1 tell application "System Events" keystroke "n" using {command down} delay 2 -- shorter delay causes the second key stroke to end up in the search keystroke "l" using {command down} keystroke "https://www.nba.com" & return delay 1 keystroke "t" using {command down} keystroke "l" using {command down} keystroke "https://www.wikipedia.org" & return delay 1 keystroke "t" using {command down} keystroke "l" using {command down} keystroke "https://www.cnn.com" & return delay 1 end tell end alfred_script The keystroke for the new window was wrong. Thanks again Jürgen Edited January 24, 2018 by JvdL Link to comment
Vero Posted January 24, 2018 Share Posted January 24, 2018 @JvdL As much as possible, avoid simulating keystrokes as this isn't a reliable way to perform actions. Wherever possible, your best bet is to use Alfred's built-in objects to do the majority of what you want, and use AppleScript or another appropriate solution where scripting is required and it can't be done using objects. Simulating keystrokes should be a last resort, as it can be really quirky. Link to comment
JvdL Posted January 24, 2018 Author Share Posted January 24, 2018 Hello Vero, I checked the workflow you mentioned, but it does open three sites in three different windows. I would like to open three sites in different tabs ... Is that possible? Link to comment
Vero Posted January 24, 2018 Share Posted January 24, 2018 @JvdL Take a look at Firefox's own preferences, as this behaviour is set by the app opening the URLs and not by Alfred. E.g. in Safari, it's in the Tabs preferences: Cheers, Vero Link to comment
GuiB Posted January 24, 2018 Share Posted January 24, 2018 (edited) I'm with @Vero about avoiding simulated keystrokes and delay using AppleScript, but in the case of Firefox and interacting with different window and tab, it is hard to get around since there's no applescript dictionary to open a new window or a new tab directly with Firefox. The things that is more reliable is to set the Firefox preference "Open links in tabs instead of new windows" to what you prefer and then it will open links how this is defined (so in your case, since you say that it opens the URL in new window with the Alfred action, then I suppose it is not ticked and set to open in a new window). So, what I recommend is to check this checkbox in the Firefox preferences (Preferences -> Tabs -> [ ] Open links in tabs instead of new windows) and then press Command+N before running a workflow like the one from @Vero or try this script: In this one, I try to limit the bad behaviour of delay and keystrokes by checking if the action was done and if not repeat and check again. So, this should work quite reliably. set urlList to {"https://www.nba.com", "https://www.wikipedia.org", "https://www.cnn.com"} tell application "Firefox" activate repeat 40 times -- gives about 4 seconds to open Firefox if not opened if frontmost then exit repeat delay 0.1 end repeat tell application "System Events" to tell application process "Firefox" set oldNbrOfWindow to (count of windows) keystroke "n" using command down repeat 20 times if ((count of windows) > oldNbrOfWindow) then exit repeat delay 0.1 end repeat end tell repeat with the_url in urlList open location the_url end repeat -- close the first default tab tell application "System Events" keystroke tab using control down delay 0.2 keystroke "w" using command down end tell end tell Edited January 24, 2018 by GuiB Link to comment
JvdL Posted January 24, 2018 Author Share Posted January 24, 2018 Hello Guib, I checked the Firefox Preferences and "Open new windows in a new tab instead" is checked. But the template workflow opens several windows anyway. But in general I agree with you both reg. keystrokes and delays. I will try your script now. Thanks for the quick answers! Jürgen Link to comment
JvdL Posted January 24, 2018 Author Share Posted January 24, 2018 Short Feedback: GuiB's workflow works perfekt and much faster than the one I mentioned. Great job! Thank you Link to comment
GuiB Posted January 24, 2018 Share Posted January 24, 2018 Great! I'm glad it worked! Link to comment
cebroski Posted November 26, 2019 Share Posted November 26, 2019 Hi GuiB, I tried using your script replacing the three websites the original poster requested with the two websites I would like Firefox to open at the same time in one window (in two separate tabs). I am a complete newbie to applescript, so I do not completely understand what it is doing yet, however, it is successfully opening the two websites I want it to open in two separate tabs. However, there is an unwanted behavior of it opening a second blank firefox window in addition to the two tabs. So, I am wondering if there is a way to modify the script so that it does not open the extra firefox window. I'm wondering if this is happening because the original script was for three websites and I only need two? Thanks for reading and for your consideration. Here is the script I am running, based on the one you wrote. Thanks, Chris set urlList to {"https://voice.google.com", "https://riot.im/app/"} tell application "Firefox" activate repeat 40 times -- gives about 4 seconds to open Firefox if not opened if frontmost then exit repeat delay 0.1 end repeat tell application "System Events" to tell application process "Firefox" set oldNbrOfWindow to (count of windows) keystroke "n" using command down repeat 20 times if ((count of windows) > oldNbrOfWindow) then exit repeat delay 0.1 end repeat end tell repeat with the_url in urlList open location the_url end repeat -- close the first default tab tell application "System Events" keystroke tab using control down delay 0.2 keystroke "w" using command down end tell end tell Link to comment
deanishe Posted November 26, 2019 Share Posted November 26, 2019 (edited) 5 hours ago, cebroski said: However, there is an unwanted behavior of it opening a second blank firefox window in addition to the two tabs. Perhaps I've misunderstood your problem. The script is designed to open the URLs in tabs in a new window. If you want to open the URLs in an existing window, delete this section of the script: tell application "System Events" to tell application process "Firefox" set oldNbrOfWindow to (count of windows) keystroke "n" using command down repeat 20 times if ((count of windows) > oldNbrOfWindow) then exit repeat delay 0.1 end repeat end tell Edited November 26, 2019 by deanishe Link to comment
GuiB Posted November 27, 2019 Share Posted November 27, 2019 @cebroski in addition to what @deanishe said, I would recommand to remove also the last part of the scipt so it doesn't close an already opened tab. I mean, remove: -- close the first default tab tell application "System Events" keystroke tab using control down delay 0.2 keystroke "w" using command down end tell However, if I understand well what you want, I think it would be better/easier/faster to use the Alfred's builtin 'Open URL' object. The idea is to have a Keyword object (or another trigger) that connect to 2 'Open URL' objects that send your specific URLs to Firefox. Here is an example workflow that should do what you want: https://d.pr/f/jsvmIY Make sure to check the "Open links in tabs instead of new windows" in the Firefox Preferences. deanishe 1 Link to comment
k_andzhanovskii Posted December 7, 2019 Share Posted December 7, 2019 Hi @GuiB, how can I change the example workflow, so after entering a keyword Alfred can randomly pick up one Open URL action? Link to comment
GuiB Posted December 7, 2019 Share Posted December 7, 2019 @KA_, you can use a "Random" node that outputs a number in the range of 1 to the number of URLs that you have and then you use a "Conditional" node to select the "Open URL" node that is linked to the selected random number. Here is an example: https://d.pr/f/yKRv5w k_andzhanovskii 1 Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now