0

I am on Ventura 13.5. I wrote a little AppleScript to convert directory tree containing Keynote .key files into Powerpoint. I am facing strange error:

When I run my script (either from terminal via osascript or directly from Script Editor) I get error pop-up from Keynote

"File1" can’t be opened right now.
Operation not permitted

I go to Finder, double-click the File1 and it opens nicely in Keynote. I close the Keynote and re-run the script, now I get error for File2. I again manually open File2, re-run and get error on File3, etc.

So basically I am getting error on every file, but when I open it manually from Finder it's OK and after that it's OK even for the script.

There must be some limit to it though. When I tried to open first 10 files, they all worked then. But when I tried more, the File1 became the issue again.

I think that at some point I have seen the issue even in Finder - error first time, then file opened fine. But I don't see that any more.

Simplest code to reproduce is this:

on run argv
    set fileToOpen to (item 1 of argv)
    
    tell application "Keynote"
        activate
        open fileToOpen
    end tell
end run

which then could be run from terminal as (use just any Keynote file)

osascript openkeynote.applescript ~/Downloads/Sample.key

Produces the error. When I open it from finder and try script again it works. When I then cp Sample.key Sample2.key the Sample2.key has the problem again.

I would be glad for advice on how to troubleshoot and fix this.

So far I have tried to remove the com.apple.quarantine xattr from the files, but that did not help. I did not try to disable System Integrity Protection (which is recommended in some places), as that feels like not right solution.

Update 1: Reproduction snippet added. My Keynote is version 12.2.1 (7035.0.161).

4
  • 1
    You are likely to get better help if you share your AppleScript code.
    – Alper
    Jul 31, 2023 at 7:26
  • @Alper thanks for suggestion, the reproduction snippet added.
    – Michal
    Jul 31, 2023 at 17:28
  • 1
    Is it possible that it was failing because fileToOpen as just a string? i.e. unlike in your solution, it doesn't have a posix file in front of it here.
    – Mockman
    Aug 1, 2023 at 3:41
  • 1
    @Mockman You are right. String is the problem, posix file or alias work fine (I would presume file too). I have updated the answer.
    – Michal
    Aug 1, 2023 at 18:21

1 Answer 1

0

It turns out that open does not work well with string paths. Those only work of the file is in the recent. However, the POSIX file or alias work fine. (for more details on those see Apple Automation Scripting Guide's Reference on Files and Folders or the other Ask Diferent Question).

Working script:

on run argv
    set fileToOpenAsString to (item 1 of argv)
    set fileToOpenAsPosixFile to POSIX file fileToOpenAsString
    set fileToOpenAsAlias to fileToOpenAsPosixFile as alias
    
    tell application "Keynote"
        activate
        
        (* This only works when the file is in File -> Open Recent menu *)
        open fileToOpenAsString
        
        (* This always works *)
        open fileToOpenAsPosixFile
        
        (* This always works *)
        open fileToOpenAsAlias
    end tell
end run

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .