0

I've searched for a couple of hours now, and every time I try a recommended fix. I still can't get anywhere. I feel like I'm missing something so obvious, my Mac is laughing at my poor attempts.

Here's the script:

tell application "System Events"
set the_folder to path to folder "dropbox" from user domain as string
set the_file to "ToDo.txt" of (POSIX path of the_folder)
set the_text to (do shell script "cat " & quoted form of (POSIX path of the_file))
return the_text
end tell

The results:

Can’t get "ToDo.txt" of (POSIX path of the_folder). Access not allowed.

It doesn't matter what the folder is, either. I've tried with Documents/Library and still always get that access issue.

Larm
  • 187
  • 12

1 Answers1

1

Here's a corrected version of your script:

    tell application "System Events"
        set the_folder to the folder "~/Dropbox"
        set the_file to the file "ToDo.txt" in the_folder

        set the_text to do shell script "cat " & ¬
            quoted form of (POSIX path of the_file as text)
    end tell

    return the_text

The points to note are as follows:

  1. Don't use Path To for folders that aren't referenced by a builtin AppleScript constant, such as home folder or desktop folder. Instead, I changed the line to a simple reference to a folder object with the specified path "~/Dropbox".
  2. Similarly, you need to place a file object specifier before stating the name of the file, otherwise all you've done is given System Events a piece of text and said that text is somewhere in the folder (which doesn't make a whole lot of sense). Now I've told System Events that it's a file and the text is the file's name, it knows exactly where to find it.
  3. Lastly, for some reason, you need to state that the POSIX path of the_file is of type class text. I don't really know why AppleScript can't see it is already text, but that's the way it is sometimes.

Now I'm going to show you another script that will do exactly what yours does:

    set the_text to read (POSIX path of ¬
        (path to home folder) & ¬
        "Dropbox/ToDo.txt" as POSIX file as alias)
CJK
  • 5,732
  • 1
  • 8
  • 26
  • Thanks so much for this! Running your script returns: error "End of file error." number -39 – Larm Jun 26 '18 at 05:20
  • And putting it in a tell applications "System Events", it returns a can't make into type error – Larm Jun 26 '18 at 05:23
  • 1
    That'll be your text file at fault. Don't put the `read` command into a `tell` block. Just leave that script for now and use the corrected version of your one. – CJK Jun 26 '18 at 05:24
  • Thanks so much! Definitely, a lot to learn in terms of Apple Scripting... Like it's great that you're basically having a conversation with a computer but I still find other languages easier to wrap my head around – Larm Jun 26 '18 at 05:26
  • 1
    A lot of very competent programmers find AppleScript difficult. Its natural language syntax is a strength in one way, but a failing in others. It actually feels very _unnatural_ to a coder, though inviting to a novice until AppleScript's fickleness makes everyone cry. – CJK Jun 26 '18 at 05:29
  • As seen in today's question :P – Larm Jun 26 '18 at 05:31
  • If I wanted that to spit out a list from each line, would that be possible? – Larm Jun 26 '18 at 06:36
  • `return paragraphs of the_text` – CJK Jun 26 '18 at 07:00
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/173796/discussion-between-liam-and-cjk). – Larm Jun 26 '18 at 07:13
  • Unlike the Finder Standard Additions accept POSIX paths so the *`POSIX file` – `alias` dance* is not needed: `set the_text to read POSIX path of (path to home folder) & "Dropbox/ToDo.txt"` – vadian Aug 19 '18 at 05:38
  • @vadian While this _should_ be true, in practice, many of the standard additions commands, `read` included, are often, and somewhat inconsistently, unacceptjng of a simple path string. In fact, `read` specifically almost always needs a `POSIX file` object. While I didn’t then need to also coerce it to an `alias`, I was in that kinda mood. My first draft of this script omitted the coercions and threw an error. – CJK Aug 19 '18 at 06:46
  • It depends on the OS version. I can't remember when Apple changed it but at least the latest 2 versions (Sierra and High Sierra) bridge the AppleScript `file` type to `NSURL` in Standard Additions. POSIX path , HFS path and alias are considered in the same way. – vadian Aug 19 '18 at 06:52