Permission Problem Opening a File in iA Writer via AppleScript

Hi all,

I tried to figure out how I could open .md files from my Obsidian vault in iA Writer via KM. Now I got a problem I couldn't solve: The macro works fine, if the file was opened once in iA Writer, but if I use it with a "new" file, I get an error message from iA Writer that the file couldn't be opened due to the lack of permissions.

The same behavior is reproducible when I run the AppleScript within the ScriptEditor and for files in different folders, not just the Obsidian vault. I have granted iA Writer full disc access and already run a "chmod -R 755" on the vault folder.

Has anyone of you a tip for me how to solve this? Thanks in advance!

Johannes

Open in iAWriter.kmmacros (3.0 KB)

Does iA Writer also give you the same error when you open a new file manually inside the iA Writer app? Or only when you run this script?

I don't have any of the apps you are talking about, and I'm not a whiz withe AppleScript, but I want to help if I can. That's the only question I can muster right now.

I can open the files in the app without problems. After I have done this, the script runs fine but just for this particular file.

Can you perform the terminal command "ls -l" on the file before and after it's been "touched" by the app. This will show the permissions, group and owner of the file. Do any of these attributes change?

Good idea! Brings some interesting things up:

Before opened in iA Writer:

MacBook-Air-von-Johannes:~ jbedenbender$ ls -l test.md 
-rw-r--r--  1 jbedenbender  staff  0 10 Feb 20:44 test.md

After opened in iA Writer:

MacBook-Air-von-Johannes:~ jbedenbender$ ls -l test.md 
-rw-r--r--@ 1 jbedenbender  staff  0 10 Feb 20:46 test.md

There are some extended attributes set, this seems to make the difference.

MacBook-Air-von-Johannes:~ jbedenbender$ xattr -l test.md 
com.apple.lastuseddate#PS: ?kb
com.apple.metadata:kMDLabel_mf6y6ml6msax5jca5fphci6zne: ??<!Ʉ.??/?ֻ???
                                                                     ??+?Q?CaFl#?u>
\???Xf?Z?li?^+????Ѕ?7m?F??:?<???N?0W??
com.apple.quarantine: 0086;62056b86;iA Writer;
net.ia.writer.selectedRange: bplist00F{0, 0}
net.ia.writer.visibleRange: bplist00F{0, 0}

I tested my script with BBEdit instead of iA Writer and it works perfectly fine on every file.

So it seems to me that this is an iA Writer problem and I will search there for a solution. Thanks for your help nonetheless!

For the sake of completeness here is the solution: I reached out for the iA Writer support and they gave me the following hints:

While support for scripting falls a bit outside our scope of support, it sounds like this is a sandboxing issue.

If the folder containing the file is added to iA Writer's Library, you will likely not see this when attempting to launch your file since iA Writer will have the necessary permissions to access the file.

Alternatively, you might be able to accomplish your intended task by using instead a shell script in Apple Script. For example, something like this for your test file:

do shell script "open /Users/jbedenbender/test.md -a \"iA Writer\""

The first solution worked out for me, but I think there will be times when the second one becomes handy.

Good job. I'm happy that you are happy. (IHTYAH.)

1 Like

Hey @jobed77,

This should work if you change appBundleID to be iA Writer's Bundle-ID.

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2022/12/14 04:50
# dMod: 2022/12/14 04:50 
# Appl: Finder, System Events
# Task: Open a File with An Application Give the App's Bundle ID.
#     : Uses the Finder to do the opening rather than the app itself.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Finder, @System_Events
--------------------------------------------------------

set fileName to "keyboardmaestro.pdf"
set containerFolderPath to "~/Downloads/"

tell application "System Events" to ¬
   set containerFolderPath to POSIX path of disk item containerFolderPath

set fileAlias to alias POSIX file (containerFolderPath & "/" & fileName)

tell application "Finder"
   set appBundleID to "com.apple.Preview"
   set appRef to application file id appBundleID
   open fileAlias using appRef
end tell

--------------------------------------------------------

I'd say there's a fair chance this will also work – by changing the file reference to an alias.

set fileName to "keyboardmaestro.pdf"
set containerFolderPath to "~/Downloads/"

tell application "System Events" to ¬
   set containerFolderPath to POSIX path of disk item containerFolderPath

set fileAlias to alias POSIX file (containerFolderPath & "/" & fileName)

tell application "iA Writer"
   open fileAlias
end tell

-Chris