Jump to content

I'm trying to create a workflow to do custom web browsing


Recommended Posts

Hi guys, I recently bought Alfred and 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 the app and give it 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

 

 

I'd love some hints on how I can make my workflow work! Thanks

Edited by shainekim
Link to comment

Hi @shainekim, welcome to the forum.

 

1 hour ago, shainekim said:

The real problem is that I cannot find how to retrieve? import? the argument as {query} and search for the argument

 

By the looks of your script, you're using Run NSAppleScript. That doesn't use {query} because Alfred calls your script with the q variable instead, which serves the same purpose.


That is to say, q is {query} when you're using Run NSAppleScript.

 

So you need to remove {query} from your script and use quoted form of q, so:

do shell script "open -a " & theApp & " 'https://youtube.com/search?q=" & quoted form of q & "'"

 

Note, I've also removed the -n parameter to open in the second example. -n doesn't open a new window, it opens an additional copy of the application. This is almost certainly not what you want to do. Apps are generally not meant to be run that way.

 

In fact, I think this whole script is a lot more complicated than it need be. What's the purpose of the if application "Google Chrome" is running clauses?

 

If you have any further questions, please upload your workflow somewhere and post a link to it, so we can see it and run it for ourselves.

 

Everything I've written above is based on memory. I haven't actually run your code because you didn't provide the workflow. As such, it's quite likely I got something wrong.

 

EDIT: I'm also moving this thread to the Workflow Help & Questions forum, which is the appropriate forum for questions about workflows.

Edited by deanishe
Link to comment

To give you another perspective... you may find it easier to use builtin Alfred object for your workflow. From my understanding, you can do what you want simply by using a Keyword object connected an Open URL object. Often, a website will give you their homepage if the search function has no argument (like if you open the url: https://www.youtube.com/search?q= ), so if it's what you want, then you don't need to check if the query is empty or not. However, if you want to direct to a different URL than you can use the 'Filter' object of Alfred to check if the query is empty or not.

 

Here is a workflow I did to show you both method in case you want to go this way: https://d.pr/f/q0uX2H

 

Also, I would recommand to use the Open URL with your default browser (since I think Chrome is your default web browser and this way it would follows your preference if you switch someday), but if you want to specify to open in Chrome then you can set it into the dropdown menu at the bottom of the Open URL object.

 

Best!

Link to comment

@GuiB

Thanks for the simple example! One thing is that I want to open the search in a new window, instead of a new tab. That's why I started to use applescript in the first place. Is there a way to improve your example so that it does that?

 

@deanishe

Thanks, I appreciate your help!

The purpose is to make sure my workflow opens a new window, both when the app is running and not running. If I execute

tell application "Google Chrome"
        activate
        open location "https://www.youtube.com"
        end tell

when the app is not running, (running it for the first time after reboot), it then activates a  new window on the homepage, and another window on Youtube. That's why I decided to put 

that do shell line. But after your help, I changed my script around, and here's my result : \

 

 

on alfred_script(q)
    if q is equal to "" then
        if application "Google Chrome" is running then
        tell application "Google Chrome"
        activate
        make new window
        open location "https://www.youtube.com"
        end tell
        else
        tell application "Google Chrome" to open location "https://youtube.com"
        end if
    else
        if application "Google Chrome" is running then
        tell application "Google Chrome"
        make new window
        open location "https://www.youtube.com/search?q=" & q
        end tell
        else
        tell application "Google Chrome" to open location "https://youtube.com" & q
        end if
    end if
end alfred_script

 

I think I'm almost done here, the only problem is 

 

1. if used q quoted form of q, then youtube searches for the actual quoted form of what I've typed in. So if I search apple, it searches for 'apple'

2. if i don't use quoted form of q, then I can't search for multiple words!

 

Oh, and I have no idea where to upload my workflow, I did save it as a file.

 

 

 

 

 

I really feel like I'm bugging you guys out with something not important at all, I really appreciate your help thanks :)

 

 

 

 

Link to comment

You need quoted form of for do shell script to make your query shell compatible. You should not use it in plain AppleScript.

 

This should work whether Chrome is running or not:

 

tell application "Google Chrome" to set URL of active tab of (make new window) to "https://www.youtube.com/search?q=" & q

 

2 hours ago, shainekim said:

I really feel like I'm bugging you guys out with something not important at all

 

You're not, but when you have a question about a workflow, it's hard for us to answer when we don't have the workflow you're talking about. So help us to help you by uploading the workflow somewhere (Dropbox?) and giving us a link, so we know what you're actually talking about.

 

 

Edited by deanishe
Link to comment

@shainekim ah, sorry, I read too fast and didn't see you wanted in a new window. For that I think there's a option in Chrome to open link in new window, but if you don't want to change your Chrome preferences for your workflow, here is some more way to do it...

 

If you have problem with the "quoted form of" then it's better to url encode your string. It's quite easy with Python, so I showed you how to pass and access the output of it in this updated workflow. Also, I've seen that Google Chrome can be asked by the "open" command to open in a new window using the argument "--new-window", so I added an example with it and an example using AppleScript to open in a new window and another example using only a single Run Script using Applescript.

 

Here is the updated workflow: https://d.pr/f/JnjG9F

 

Have a look at the keywords: "yy3", "yy4" and "yy5"

Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...