ICloud – AppleScript Opening iCloud Documents

applescriptfindericloudnumbersterminal

I am trying to write an AppleScript to open a Numbers document, which is stored in the Numbers folder on my iCloud drive, so that I can access a cell value and create a reminder with it (or otherwise notify myself of this value).

When I try to access the file directly using the symlink to my iCloud I created in my home folder, Numbers says the file doesn't exist.

I found the following on a blog post, though I am not sure I am using it right.

set the defaultDestinationFolder to "/Users/luketimothy/Desktop"
set filePath to "/Users/luketimothy/Library/" & "Mobile Documents:com~apple~Numbers:Documents:Budget.numbers"


tell application "Numbers"
    activate
    try
        open filePath
    on error

    end try
end tell

This script also results in a "file doesn't exist" error.

I tried copying the file into my Documents folder, but I got the same error (and the file doesn't show up in the Documents folder in finder either, even though if I try to copy it again I am asked if I wish to overwrite)

I can't seem to access the Numbers folder in my iCloud folder from the terminal either, which might allow me to use pwd to get an absolute path.

What can I do? Is there some restriction on access to this Numbers folder?

Best Answer

You’re trying to define the filePath variable by combining a Posix path string with an HFS string.

Try defining the filePath variable like this…

set filePath to (path to library folder from user domain as text) & ¬
    "Mobile Documents:com~apple~Numbers:Documents:Budget.numbers"

tell application "Numbers"
    open alias filePath
end tell

OR.. My Personal Preference

property theDocument : "Budget.numbers"

set filePath to ((path to library folder from user domain as text) & ¬
    "Mobile Documents:com~apple~Numbers:Documents:" & theDocument)

tell application "Finder"
    if not (exists of alias filePath) then
        tell current application to display dialog "The File: " & theDocument & ¬
            " Does Not Exist" buttons {"Cancel", "OK"} default button "OK"
        return
    end if
end tell

tell application "Numbers"
    open alias filePath
end tell

If you’re not quite sure how to correctly define a file path, you can always use the following Apple Script code in a separate script editor document to correctly retrieve the file paths

set filePathAsAlias to (choose file with prompt "choose file") -- Returns The File Path As An Alias

OR

set posixPathOffilePath to POSIX path of (choose file with prompt "choose file") -- Returns The POSIX Path Of The File