Jump to content
Bhishan

[SOLVED] Workflow to split a list and insert list elements followed by some keystrokes

Recommended Posts

I have long string of python imports like this:

 

# usual imports
import os
import sys



# data manipulation
import numpy as np
import pandas as pd

 

 

While working in Jupyter notebook, when I paste something and hit "cmd enter" it will go to next cell and again we can do paste and hit enter.

 

How can it be done using Alfred?

 

I have tried this so far:

import sys
import os
import time

s = """
# usual imports
import os
import sys

# data manipulation
import numpy as np
import pandas as pd
"""

lst = s.lstrip().rstrip().split('\n#')


for i,l in enumerate(lst):
    x = l if i == 0 else '#'+l
    os.system("tell application system events to keystroke v holding command")
    time.sleep(0.2)
    os.system("tell application system events to keystroke return holding command")

 

This is a starting idea, but the code is not working. Maybe I have something to change in applescript or follow other ideas.

My question to experts is how to make it viable?

 

I have shared my primary workflow here:  https://github.com/bhishanpdl/Shared/blob/master/Alfred_questions/a a a split the imports.alfredworkflow?raw=true

Edited by Bhishan

Share this post


Link to post
9 hours ago, Bhishan said:

but the code is not working

 

No, it wouldn’t. What are you actually trying to achieve?

 

Your description is unclear and your code makes no sense.

Share this post


Link to post

Apologies for unclear question.

 

Here is what I am trying to achieve:

1.  Lets say I have a list   mylist =  ["#hello", "#world"] obtained from clipboard which in fact contain arbitrary number of elements.

2. print #hello  to given app and simulate keystroke cmd + enter

3. print #world to given app and simulate keystroke cmd+enter

 

If I already know how much elements in list, I can append alfred workflow object "output > keyboard combination" one by one, but I don't know how many elements are 

in the list and I want to hit keyboard keys cmd+enter after pasting list elements each time.

 

The os.system("...") command is just the pseudocode,  obviously it does not work.

Share this post


Link to post
On 9/29/2019 at 5:01 AM, Bhishan said:

contain arbitrary number of elements.

 

Ah, right. That explains why you haven't just used a snippet, I guess.

 

On 9/29/2019 at 5:01 AM, Bhishan said:

but I don't know how many elements are 

in the list and I want to hit keyboard keys cmd+enter after pasting list elements each time.

 

You can use an External Trigger. You can call that as many times as you like from your code.

 

Otherwise, you'll need to call pbcopy to put the text on the clipboard. That's the obviously missing part from your script.

 

On 9/27/2019 at 11:51 PM, Bhishan said:

os.system("tell application system events to keystroke return holding command")

 

You can't pass AppleScript to os.system(). You have to pass it to /usr/bin/osascript, which is the command-line program that understands AppleScript.

 

Also, you should use subprocess.call() (or subprocess.check_call()), not os.system(). It's much easier to use, as you don't have to worry about escaping your command.

 

On 9/27/2019 at 11:51 PM, Bhishan said:

lst = s.lstrip().rstrip()

 

Just use s.strip().

 

On 9/27/2019 at 11:51 PM, Bhishan said:

tell application system events to keystroke return holding command

 

tell application "System Events" to keystroke v using command down

 

But it's generally better to use Alfred's built-in keystroke simulation, as it's more reliable.

Share this post


Link to post

@deanishe   Thanks for the suggestion.

Now I can paste the last part of the list and also hit "shift-enter". But still I am sure how to make this in a for loop so that it works for all the parts of the list.

 

Example:

print(3)
# comment
print(5)

I copied this text to the clipboard and ran my updated workflow: https://github.com/bhishanpdl/Shared/blob/master/Alfred_questions/a a a split the imports updated.alfredworkflow?raw=true

 

It gives:

"""

# some comment
print(5)

"""

And also hits "shift enter" (this means goes to next cell in jupyter notebook).

 

How to make this work for all the list components like this:

 

1. print first part of split of #

print(3)

 

2. hit shift-return 

This goes to downward cell in jupyter notebook

 

3. again paste next part of # split

# some comment
print(5)

 

4. again hit keybord "shift-enter"

this creates new cell in jupyter notebook

 

 

 

I am not sure how to create External Triggers for this workflow.

Help is much appreciated. Thanks.

 

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...