I think so. And I realized in my screenshot that I'm using 'execute text script' rather than linking to the file. I tried linking to the file and it didn't work.
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.")
I should have asked this sooner: What are you seeing in the Engine.log file?
You can get to the log file using Help>Open Logs Folder. When you double-click it, it should open the file in the Console application. After you open it, try the macro again and then report back with the error(s) you are seeing.
2023-09-22 12:07:25 Execute macro “Python sort bullets by character count” from trigger The Hot Key ⌃⌥⌘L is pressed
2023-09-22 12:07:26 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 3, in
import pyperclip
ModuleNotFoundError: No module named 'pyperclip'
2023-09-22 12:07:26 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 3, in
import pyperclip
ModuleNotFoundError: No module named 'pyperclip'. Macro “Python sort bullets by character count” cancelled (while executing Execute Shell Script).
2023-09-22 12:07:29 Execute macro “Activate Application Switcher” from trigger The Hot Key ⌘Tab is pressed
========
This is odd because I'm able to run the script in Terminal by entering "python3 ". So I'm not sure why pyperclip isn't found?
My Python experience is minimal and rusty, so I'm getting a bit over my skis. Disclaimers stated...
Python seems to be having a problem accessing the module pyperclip. I think this is related to the sys.path and you could add something like this to your code:
And when I run the script, the engine.log gave me this error:
2023-09-22 15:44:30 Execute macro “Python sort bullets by character count” from trigger The Hot Key ⌃⌥⌘L is pressed
2023-09-22 15:44:31 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 7, in
text = pyperclip.paste()
AttributeError: module 'pyperclip' has no attribute 'paste'
2023-09-22 15:44:31 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 7, in
text = pyperclip.paste()
AttributeError: module 'pyperclip' has no attribute 'paste'. Macro “Python sort bullets by character count” cancelled (while executing Execute Shell Script).
======
So we're making progress! I searched online for this issue and it seems that sometimes people have two files called pyperclip.py. I've confirmed that I don't. I also reinstalled pip.