I am getting an 'Operation not permitted' when running Python file with AppleScript

I created a Service that writes a selected word to an Excel file. Running the .py file from PyCharm or Terminal works fine, but whenever I try to execute it with AppleScript I get a PermissionError. Why is this?


AppleScript

on run {input, parameters}
  display dialog (input as text)
  set venvPath to "/Users/ben/PycharmProjects/Excel_writer/venv/bin/python3.8"
  set pyScript to "/Users/ben/PycharmProjects/Excel_writer/writer.py"
  do shell script venvPath & " " & pyScript & " " & input
  return input
end run


The action “Run AppleScript” encountered an error: “Traceback (most recent call last):
  File "/Users/ben/PycharmProjects/Excel_writer/writer.py", line 26, in <module>
    insert_to_excel(str(sys.argv[1]))
  File "/Users/ben/PycharmProjects/Excel_writer/writer.py", line 17, in insert_to_excel
    book = load_workbook(file_path)
  File "/Users/ben/PycharmProjects/Excel_writer/venv/lib/python3.8/site-packages/openpyxl/reader/excel.py", line 312, in load_workbook
    reader = ExcelReader(filename, read_only, keep_vba,
  File "/Users/ben/PycharmProjects/Excel_writer/venv/lib/python3.8/site-packages/openpyxl/reader/excel.py", line 124, in __init__
    self.archive = _validate_archive(fn)
  File "/Users/ben/PycharmProjects/Excel_writer/venv/lib/python3.8/site-packages/openpyxl/reader/excel.py", line 96, in _validate_archive
    archive = ZipFile(filename, 'r')
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/zipfile.py", line 1251, in __init__
    self.fp = io.open(file, filemode)
PermissionError: [Errno 1] Operation not permitted: '/Users/ben/Desktop/Ben_Test.xlsx'”


I've also added Automator and Finder to System Preferences > Security > Privacy > Accessibility. I've tried adding Automator to Full Disk Access as well as changing the permissions to 777 on the .xlsx file but I still get the error.


Posted on May 29, 2020 6:57 PM

Reply

Similar questions

6 replies

May 30, 2020 7:51 PM in response to FreddEng22

The input value in the Run AppleScript was coming into the action as "alias Macintosh HD:Users:yourname ..." (Apple HFS format), and that is about as alien to a Python sys.argv as you can get. Thus the POSIX path of (input as text) changed that to "/Users/yourname/..." which is the expected path format, and digestable by Python.


The Automator Run Shell Script action only knows about the default Apple System Python 2.7.n distribution (/usr/bin/python), and any part of your solution that does not explicitly request python3, and uses python instead will likely invoke the System Python 2.7.n interpreter. That causes mayhem in code expecting Python3.


Make certain that your openpyxl module is current and intended for Python3.


You may have to be very patient while you recheck your code. Traceback is not going to just hand the answer to you.

May 30, 2020 7:44 AM in response to FreddEng22

Not accounting for your Python virtual environment and possible other coding issues, you will need the following change in syntax to send in the expected UNIX path format:


do shell script venvPath & " " & pyScript & " " & POSIX path of (input as text)


The next question is why you are using a Run AppleScript action instead of a Run Shell Script action that would pass the argument in the correct (UNIX format)? Your input manipulations would be "${1}" instead.


My Python3 virtual environment is ~/py3venv and to activate it:

source ~/py3venv/bin/activate && cd ~/py3venv
./foo.py


My Python3 scripts have the following, first line to avoid the overly long invocation commands that you exhibit in your run AppleScript action:

#!/usr/bin/env python3






May 30, 2020 3:30 PM in response to VikingOSX

Thank you for the reply. I think the issue is with either the `openpyxl` package or the built in `zipfile.py`.


I made the initial syntax change (adding POSIX path of ... ) and still got the error.


Then I swapped out the AppleScript for Run Shell Script, used your setup, gave execution permission to `writer.py` but I got the same error.


source /Users/ben/PycharmProjects/Excel_writer/venv/bin/activate && cd /Users/ben/PycharmProjects/Excel_writer
./writer.py "${1}"


error:

The action “Run Shell Script” encountered an error: “Traceback (most recent call last):
  File "./writer.py", line 28, in <module>
    insert_to_excel(str(sys.argv[1]))
  File "./writer.py", line 19, in insert_to_excel
    book = load_workbook(file_path)
  File "/Users/ben/PycharmProjects/Excel_writer/venv/lib/python3.8/site-packages/openpyxl/reader/excel.py", line 312, in load_workbook
    reader = ExcelReader(filename, read_only, keep_vba,
  File "/Users/ben/PycharmProjects/Excel_writer/venv/lib/python3.8/site-packages/openpyxl/reader/excel.py", line 124, in __init__
    self.archive = _validate_archive(fn)
  File "/Users/ben/PycharmProjects/Excel_writer/venv/lib/python3.8/site-packages/openpyxl/reader/excel.py", line 96, in _validate_archive
    archive = ZipFile(filename, 'r')
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/zipfile.py", line 1251, in __init__
    self.fp = io.open(file, filemode)
PermissionError: [Errno 1] Operation not permitted: '/Users/ben/Desktop/Ben_Test.xlsx'”


The extra +2 to line numbers is only because I added the `#!/usr/bin/env python3` and a newline.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

I am getting an 'Operation not permitted' when running Python file with AppleScript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.