oposx Posted May 10, 2021 Share Posted May 10, 2021 Hello all. So I've been trying to set up a workflow (since I couldn't find any online) that lets me search the web using a new private window in Safari. If it could work exactly like the default web search, but in a private window, that would be great, but I am having some trouble. First off I am not a programmer, and all the related answers I found online were using Applescript, so that's what I've been trying to use. Yeah I realize it isn't the best of languages, but I had nothing else to go on unfortunately. What I did was basically take a similar script I found online and try to modify it to search the web after opening the private window. I was able to do so, but now my next challenge has been trying to use Alfred's variables inside my Applescript, and I am totally lost. This is the script below: on alfred_script(q) tell application "Safari" activate tell application "System Events" tell process "Safari" keystroke "N" using {shift down, command down} delay 0.5 tell application "Safari" to search the web for (system attribute "search") end tell end tell end tell end alfred_script Where I have "(system attribute "search")", is where to search using the terms I input into Alfred. I configured my keyword to be "gp", so ideally I'd just type "gp hello" and it'd search for "hello" in a new, private window in Safari. What is happening, though, is that it is just opening a new private window without any input at all. I assume I am doing something wrong with the variable in Alfred and/or Applescript, but I haven't been able to find any help online and everything I've tried hasn't worked. Right now I have the connector between the Keyword and the Applescript being an Arg and Vars, with only 1 new variable created in it (name: search, value: {query}). I've also tried configuring this variable using the Workflow Environment Variables and still, nothing. Please help. Here's the link to the workflow, as well. Thanks. Link to comment
giovanni Posted May 11, 2021 Share Posted May 11, 2021 (edited) hi @oposx, welcome to the forum! Run script is recommended over Run NSAppleScript. Setting input as argv, `argv as text` will achieve what you want. Try this version Also, if you want to avoid the keystrokes, Applescript etc, and are ok with using Chrome, this bash command will also work: open -na "Google Chrome" --args --incognito ${query} Edited May 11, 2021 by giovanni Link to comment
deanishe Posted May 11, 2021 Share Posted May 11, 2021 8 hours ago, oposx said: First off I am not a programmer, and all the related answers I found online were using Applescript, so that's what I've been trying to use. Yeah I realize it isn't the best of languages, but I had nothing else to go on unfortunately. What I did was basically take a similar script I found online and try to modify it to search the web after opening the private window. I was able to do so, but now my next challenge has been trying to use Alfred's variables inside my Applescript, and I am totally lost. AppleScript is fine for making applications do things. You don't have many options, and AppleScript is the "native" one, so often the best choice. The reason it isn’t working is because of Run NSAppleScript. Workflow variables aren’t available in Run NSAppleScript because the script is run in Alfred’s own process, not a separate one with its own environment Alfred can set variables in. If you move your code to a Run Script Action with Language = /usr/bin/osascript (AS), it should work. Note: You also need to change the function signature. Run Script uses on run argv, not on alfred_script(q). But you don’t actually need any variables at all. They’re for passing extra information around. The search query is passed around separately (known as “arg”, “Argument” or “{query}”). So you can remove the Arg & Vars from your script: the search query you entered is the value of the q in on alfred_script(q) or first item of argv in a Run Script AppleScript. You shouldn't use Run NSAppleScript, like @giovanni says. It behaves weirdly (i.e. code that runs in Script Editor or via /usr/bin/osascript doesn't work in a Run NSAppleScript and nobody knows why…) and it's run on Alfred's main thread, so Alfred will be unresponsive as long as your script is running. Use a Run Script instead. These are run in separate processes, so they won't lock up Alfred. Also workflow variables work with them. Here's a fixed version of your workflow. Link to comment
oposx Posted May 11, 2021 Author Share Posted May 11, 2021 Hey @giovanni and @deanishe! First off thanks a lot for the help, I knew I was doing something very wrong lol. 13 hours ago, giovanni said: Also, if you want to avoid the keystrokes, Applescript etc, and are ok with using Chrome, this bash command will also work: Thanks, I'll keep this in mind for the future, but in this specific case I want to use Safari, been trying to avoid Chrome :). This much simpler and to the point though, than what I had made with Applescript, thanks either way! Also, interesting that Run NSApplescript doesn't work with variables, good to know for future workflows. If isn't asking too much, why though? Is there any specific situation where it'd be better to use Run NSApplescript over "Run Script Action with Language"? 6 hours ago, deanishe said: But you don’t actually need any variables at all. They’re for passing extra information around. The search query is passed around separately (known as “arg”, “Argument” or “{query}”). So you can remove the Arg & Vars from your script: the search query you entered is the value of the q in on alfred_script(q) or first item of argv in a Run Script AppleScript. Welp, I did not know this lol. Thanks for clearing this up, I was very confused as to where I should create the query variable and in the end I didn't even need it. Again, thank you both for the help! Link to comment
deanishe Posted May 12, 2021 Share Posted May 12, 2021 17 hours ago, oposx said: Is there any specific situation where it'd be better to use Run NSApplescript over "Run Script Action with Language"? Showing a notification is the only one I'm aware of. Run NSAppleScript shows Alfred's icon, while Run Script shows an ugly "binary executable" one. giovanni and oposx 2 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