The import pyperclip
line should be after the sys.path.append...
line. Also the path in single quote should not include the file name, i.e., remove /pyperclip.py
at the end.
Sorry... here's the code now:
It's giving this error in engine.log:
2023-09-22 19:20:54 Execute macro “Python sort bullets by character count” from trigger The Hot Key ⌃⌥⌘L is pressed
2023-09-22 19:20:54 Execute a Shell Script failed with script error: Traceback (most recent call last):
File "/Users/adamcosta/Documents/Folder_for_python_scripts/sort_text_by_character_count.py", line 9, in
text = pyperclip.paste()
AttributeError: module 'pyperclip' has no attribute 'paste'
2023-09-22 19:20:54 Execute a Shell Script failed with script error: Traceback (most recent call last):
File "/Users/adamcosta/Documents/Folder_for_python_scripts/sort_text_by_character_count.py", line 9, in
text = pyperclip.paste()
AttributeError: module 'pyperclip' has no attribute 'paste'. Macro “Python sort bullets by character count” cancelled (while executing Execute Shell Script).
I'm out of ideas. Hopefully someone else can weigh in.
Hello,
From error message it is telling you that the paperclip namespace doesn’t have a paste attribute.
It looks to me like you need to tell Python the namespace alias.
From the examples of using Paperclip library. You can see they alias the paperclip library “as pc”
————
# importing the library
import pyperclip as pc
text1 = "GeeksforGeeks"
# copying text to clipboard
pc.copy(text1)
# pasting the text from clipboard
text2 = pc.paste()
print (text2)
————
Try that in your code. It shouldn’t matter, but worth a try.
Also try running the example just from the command line outside KM to see it work there.
Another thing to make sure you don’t have is a file named pyperclip.py or pyperclip,pyc in the same folder as your script as it’ll think that is the pyperclip module. If you do rename that file to pyperclipdemo.py or pyperclipdemo.pyc respectively.
Then try your program again.
If that doesn’t work, then do a
pip uninstall pyperclip
and then
pip install pyperclip
Then try the demo script from above and name it like abc.py and run it and see if it works.
No worries, @_jims. Thank you so much for your help!
Thanks for this, @staypufd. I ran it in Terminal it gave me a similar error:
adamcosta@Adams-MacBook-Pro Folder_for_python_scripts % python3 import_example.py
/Users/adamcosta/Documents/Folder_for_python_scripts/pyperclip.py
Traceback (most recent call last):
File "/Users/adamcosta/Documents/Folder_for_python_scripts/import_example.py", line 7, in
pc.copy(text1)
^^^^^^^
AttributeError: module 'pyperclip' has no attribute 'copy'
Two more updates:
I don't have another pyperclip file.
I reinstalled pyperclip.
Still the same issue.
Stepping back a bit here,
what are you aiming to produce ?
The lines in the clipboard sorted by ascending length ?
Yes, that's correct.
Edit: here's an example @ComplexPoint
I want to copy this:
— New Tab - Command + T
— Close Current Tab - Command + W
— Reopen Closed Tab - Command + Shift + T
— Switch to Next Tab - Command + Option + Right Arrow
— Switch to Previous Tab - Command + Option + Left Arrow
— Jump to a Specific Tab (1-8) - Command + 1-8
— Jump to Last Tab - Command + 9
and repaste it as this:
— New Tab - Command + T
— Jump to Last Tab - Command + 9
— Close Current Tab - Command + W
— Reopen Closed Tab - Command + Shift + T
— Jump to a Specific Tab (1-8) - Command + 1-8
— Switch to Next Tab - Command + Option + Right Arrow
— Switch to Previous Tab - Command + Option + Left Arrow
... without having to go the Terminal.
Run the following in Terminal app:
echo $PATH
you would get a result like:
/opt/homebrew/sbin:/usr/local/bin:
Create a variable in KM (if it doesn't exist) named ENV_PATH
and paste the contents there:
Now see if the following python script works:
#!/usr/bin/env python3
import pyperclip
# Get text from clipboard
text = pyperclip.paste()
# Split the text into lines and remove leading/trailing spaces
lines = [line.strip() for line in text.splitlines()]
# Sort the lines by character count (including spaces)
sorted_lines = sorted(lines, key=lambda x: len(x))
# Join the sorted lines back together
sorted_text = '\n'.join(sorted_lines)
# Copy the sorted text to the clipboard
pyperclip.copy(sorted_text)
print("Sorted text (including spaces) copied to clipboard.")
However, I would recommend a python version which doesn't rely on dependencies:
#!/usr/bin/env python3
import os
s = os.environ["KMVAR_localSample"]
def main():
value = sorted(
s.splitlines(),
key=len
)
print("\n".join(value))
if __name__ == '__main__':
main()
Download Macro(s): Clipboard lines sorted by ascending length.kmmacros (2.4 KB)
Macro-Notes
- Macros are always disabled when imported into the Keyboard Maestro Editor.
- The user must ensure the macro is enabled.
- The user must also ensure the macro's parent macro-group is enabled.
System Information
- macOS 13.4
- Keyboard Maestro v10.2
@unlocked2412 thanks for your help.
- This is the path it returned (which seems strangely long, with lots of different apps):
/Applications/Sublime Text.app/Contents/SharedSupport/bin:/Applications/Sublime Text.app/Contents/SharedSupport/bin:/Library/Frameworks/Python.framework/Versions/3.11/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin
adamcosta@Adams-MacBook-Pro Folder_for_python_scripts %
- I pasted that into a variable:
- And ran your first script in Terminal. Here's the error:
This macro worked! Thank you so much!!!!!!
If I may ask, what is the secret to this? How can I run Python scripts going forward?
You are welcome, @cozandeffect1.
You shouldn't include your Terminal prompt in your selection. Perhaps you highlighted a bit more than intended ?
Just by chance, you don't have a .py
file named pyperclip.py
?
I have to go out of my house, I will try to write up something tomorrow about that.
I do have a pyperclip.py file now (which was created yesterday). But I saw this error before that happened (you can see it higher up this thread if you're interested).
No rush on the explanation, though I'm eager to run more Python scripts with KM..
Thank you again and have a great day!
Sorry, I'm not sure what you mean? (I'm very new to coding.)
The terminal prompt is a brief text string that appears at the start of the command line in a terminal or console window, indicating that the terminal is ready to accept a command. It typically provides contextual information such as the username, hostname, current directory, or system status, and ends with a special character.
For instance, in your case, the terminal prompt is adamcosta@Adams-MacBook-Pro Folder_for_python_scripts %
. Here, 'adamcosta' is the username, 'Adams-MacBook-Pro' is the hostname, and 'Folder_for_python_scripts' is the current directory. The '%' symbol signifies that the terminal is ready to accept commands, and it also indicates that you're likely using the Z shell (zsh).
So, you should include the same list of paths that appear in the output of echo $PATH
command. The terminal prompt is clearly not one of those paths (it's not a path at all).