Jump to content

Passing args from "Script Filter" to "Run Script" works weird


Recommended Posts

I'm modifying the Alfred Notion Workflow: https://github.com/wrjlewis/notion-search-alfred-workflow

 

Here's my modified version: https://www.dropbox.com/s/5gk76u8aln22opc/Notion Search_JZ.alfredworkflow?dl=0

 

Basically trying to pass whatever user's chosen from "Script Filter" to "Run script"

image.png.2df8061e9e6df1b92183c2182f8cc32c.png

 

Unfortunately, it's not working

 

  1. with AppleScript - fails because Alfred adds trailing white space (stripping it out doesn't work 🤔)
  set theQuery to item 1 of argv

    tell application "System Events" to tell process "Notion"
        click menu item "New Window" of menu 1 of menu bar item "File" of menu bar 1
    end tell


  return theQuery
end run

image.png.6cefb28969879e5bd96dd1f36e709209.png

 

  1.  tried a Python Run Script (which opens 2 New Windows 🤯)
import sys
from subprocess import Popen, PIPE

query = sys.argv[1]

script = '''
    # activate application "Notion"
    tell application "System Events" to tell process "Notion"
        click menu item "New Window" of menu 1 of menu bar item "File" of menu bar 1
    end tell'''

p = Popen('osascript', stdin=PIPE, stdout=PIPE, stderr=PIPE)
p.communicate(script)

sys.stdout.write(query)

 

Any idea:

1) Why the AppleScript adds trailing spaces?
AND 
2) Why the Python Run Filter passing parameter using `sys.stdout.write()` to a Python Run Script to Open URL opens the URL twice?

 

Thanks!

Link to comment
1 hour ago, johnathanz said:

with AppleScript - fails because Alfred adds trailing white space

 

Alfred doesn’t add it, osascript does. It’s just another AppleScript weirdness.

 

1 hour ago, johnathanz said:

Why the Python Run Filter passing parameter using `sys.stdout.write()` to a Python Run Script to Open URL opens the URL twice?

 

What do you mean "it opens the URL twice"? I've never seen an Open URL do that, and your log says it's only being called once.

 

What behaviour makes you say the URL is being opened twice? Notion opening two windows?

 

In that case, it's probably a timing screw-up in your code. Simulating a menu click isn't synchronous. That code will return before Notion actually responds to the click. It's likely that your Open URL command is reaching Notion before it has acted on the "New Window" menu click, so it's opening one window in response to your "Open URL" action and then creating another showing the last active note (the one you just told it to open via the Open URL action) as a result of the "New Window" command.

 

Try removing the "New Window" bit.

Edited by deanishe
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...