Bhishan Posted September 27, 2019 Share Posted September 27, 2019 (edited) 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 October 14, 2019 by Bhishan Link to comment
deanishe Posted September 28, 2019 Share Posted September 28, 2019 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. Link to comment
Bhishan Posted September 29, 2019 Author Share Posted September 29, 2019 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. Link to comment
deanishe Posted September 30, 2019 Share Posted September 30, 2019 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. Link to comment
Bhishan Posted October 1, 2019 Author Share Posted October 1, 2019 @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. Link to comment
deanishe Posted October 1, 2019 Share Posted October 1, 2019 This workflow should kinda do what you want. Note, you also have to sleep in the Python code because calling an External Trigger returns immediately. It doesn't wait for the trigger to finish. Bhishan 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