Unable to save file in iCloud

I try to save an attachment from an email to a folder in iCloud.
First I put the path to the mobile documents folder:
set MobileDocumentsPath to POSIX path of (path to the library folder from the user domain) & "Mobile Documents/"
Then I set the variable savePath: set savePath to MobileDocumentsPath & originalName
The savePath variable is:
/Users/jjn/Library/Mobile Documents/xxxxxxxxxx.20161026.html

Then I get the following error message (in Dutch):
Om bevoegdheden te bekijken of te wijzigen, selecteer je het onderdeel in de Finder en kies je ‘Archief’ > ‘Toon info’. (-10000)
Translated is that about:
"To view or change permissions, select the item in the Finder and choose ‘File > Get Info’. (-10000)

It might be a trivial problem but I don’t see the solution.
Manually moving the Attachment is no problem.
Saving the attachment in a folder on the Mac with similar code also gives no problem.

You can’t save into Mobile Documents directly like that – it’s not really a folder.

If you look in the Finder:

you will see it even appears differently (no arrow).

I’m not sure what the work-around would be.

On 10.11, I’m pretty sure something of this form works:

<path to user>:Mobile Documents:com~apple~CloudDocs:<filename>

It works if you save to the “Documents” folder (at the end of the App’s cloud path).

For example this works:

set thePath to (path to library folder from user domain as text) & "Mobile Documents:com~apple~TextEdit:Documents:"
set docName to "test.txt"
tell application "TextEdit"
  make new document at front
  tell front document
    set its name to docName
    save in file (thePath & docName)
    close
  end tell
end tell

Thanks Tom. The file is indeed saved in the map Teksteditor (Dutch name) but I want save to the file in a own folder, by example: documents/private.
Thanks Shane. There is a work-around via Hazel. If Hazel succeeds then it must be possible via Applescript or am I totally wrong.
Thanks Phil, I have tried the way you suggest but with no result.

If you see the App’s folder in the “iCloud Drive” you can simply create a subfolder:

Then change the path in the script accordingly and it should work:

set thePath to (path to library folder from user domain as text) & "Mobile Documents:com~apple~TextEdit:Documents:Subfolder:"

The “Subfolder” is in fact a subfolder of …/TextEdit/Documents. (The App’s Documents folder is displayed as the App’s root folder in the iCloud view in the Finder.)

Tom, I want save the file in a subfolder of a folder on the same level as the TextEdit folder.

I have found an work-around that fits me:

I save the attachment in an folder (user/jjn/Bijlagen) on my Mac with the statement:

save theAttachment in file (savePath)

Then I move the file to the cloud folder Documents/Prive

set MobileDocumentsPathUnix to "~/Library/Mobile\\ Documents/com~apple~CloudDocs/Documents"

do shell script ("mv ~/Bijlagen/xxxxxxxxx.20161027.html " & MobileDocumentsPathUnix & "/Prive")

Perhaps somebody knows a faster way, suggestions are welcome!

It would be good to know your system version, because the iCloud Drive stuff has changed over the last years.

For example, on my Mac here (10.12, with iCloud Documents Sync enabled) the Documents folder in com~apple~CloudDocs is just a link to the regular Documents folder:

So, your script wouldn’t make much sense for me, and vice versa.

My system version is also 10.12.1 with iCloud Documents Sync enabled
and the Documents folder is also a link the regular Documents folder:

With only AppleScript I can’t move the file, with the shell statement I have success. It’s my first script and I haven’t time to search deeper for an ‘AppleScript solution’.
I’m glad to have now a flexible solution, so I can continue with the script.

Then I would simply use the “regular” path ~/Documents/Prive, or, in AppleScript: (path to documents folder from user domain as text) & "Prive:"


Edit, PS:

This would be your File Moving action as AppleScript:

set documentsFolder to path to documents folder from user domain as text
set homeFolder to path to home folder as text
tell application "Finder" to move file (homeFolder & "Bijlagen:xxxxxxxxx.20161027.html") to folder (documentsFolder & "Prive")

Tom, you are completely right. Thanks very much.
The whole topic is a non-topic and I’ll try to remove it because it causes confusion and I’m a little ashamed about it.

It’s nothing uncommon that a topic becomes a “non-topic” once you know the solution. It’s not uncommon either that the solution turns out to be simpler – or different – than thought; that’s rather a good thing, nothing to be ashamed off. IMO.

Besides that, the topic holds some information that might be useful:

  • When iCloud stuff is involved in a script you should take account of your system version and your iCloud settings.
  • It wasn’t obvious to me either that com~apple~CloudDocs/Documents is just a symlink to ~/Documents. Initially I thought it was a hard-linked folder (same inode number), like they use to do it in the Time Machine backups.

Johannes,

I agree with Tom in his reply to your post. I posted something not to bright about putting pipes around something that once Shane posted a reply I instantly see what I did was wrong. But there might well be other people who would think of what I thought and if they read the posts on that they will know to not do that. Posting is about sharing what you know, and learning from others. There’s nothing to be embarrassed about. Like Tom said people can learn from the posts in this topic.

Bill

This topic is going to recur repeatedly with Sierra and iCloud syncing of Documents and Desktops folder. This thread is a great reference.

The script functions well* until installing the last OS Siera update (10-12-2 update).

Now I can save an attachment in the Documents root but not in the folders.

Then I get again the following error message (in Dutch):
Om bevoegdheden te bekijken of te wijzigen, selecteer je het onderdeel in de Finder en kies je ‘Archief’ > ‘Toon info’. (-10000)
Translated is that about:
"To view or change permissions, select the item in the Finder and choose ‘File > Get Info’. (-10000)

So the first two commands succeed ant the third gives the error:

save theAttachment in file ("Macintosh HD:Users:jjn:Bijlagen:test.pdf")
save theAttachment in file ("Macintosh HD:Users:jjn:Documents:test.pdf")
save theAttachment in file ("Macintosh HD:Users:jjn:Documents:Bijlagen:test.pdf")

What’s going wrong and how can I repair this?

Today (2017-01-04) I tried it again and now also the first save failed.
Perhaps it’s an setting on my iMac but I haven’t changed anything. It’s a mystery for me …

Update 2017-01-05
I have found a solution. I save the attachment to a subfolder of the folder Downloads (that gives not the problems mentioned above) and move the attachment afterwards to the subfolder of the folder Documents (in iCloud).

*) The problem in the topic ‘Emails not processed by script’ isn’t resolved.