Read from file

Discussion in 'Mac Programming' started by Aouttier, Apr 27, 2016.

  1. Aouttier macrumors newbie

    Mar 3, 2016
    Good evening,

    I want to read a text file. I use the code:
    set Mail to "Macintosh HD:Users:Amelie:Documents:Heron:DB:BronDocumenten:Mail:MailBwhr1.txt"
    set MailTekst to (read file Mail)
    display dialog MailTekst
    But, I get the error -39: end of File error

    Does someone know what is wrong?

    Thanks in advance
  2. chown33 macrumors 604

    Aug 9, 2009
    Break It Down.
    Post Details.

    1. Perform the commands one at a time in AppleScript Editor. Look at the result of each command.

    For example, the result of
    set MailTekst to (read file Mail)​
    should be the contents of the file as a quoted string. If it's not that, then you need to look more closely at the file and what's in it.

    2. How long is the .txt file? Exactly what's in it? How was it created?

    3. What OS version?

    When I use an example text file on my Macs, the code works. My example text file is only a couple of paragraphs, and I made it in and saved it as text.

    Without further details of exactly what's happening on your machine, all anyone can do is guess.
  3. Aouttier thread starter macrumors newbie

    Mar 3, 2016

    Thank you very much for the advice. Apparently, the file was to long. When i made the file shorter, it worked. So, that is solved.

    But, now I have a new problem. I was trying to write a script in Filemaker that read a text file and then put it into a mail and send it. In Filemaker, it is possible to use a text file to make a mail but not to make the name of the textile dependent of a parameter. So, I would like to use applescript to read the text file and put the content into a field. But, the contents of the mail is to long to do it that way.

    Is there another work around for reading a text file?
  4. chown33, Apr 28, 2016
    Last edited: Apr 28, 2016

    chown33 macrumors 604

    Aug 9, 2009
    I think you're focussed on trying to solve the wrong problem.

    You want to send the contents of a text file as an email. This is the goal. How you accomplish the goal is only the means of achieving that goal. The means is not the goal. It's just one way of reaching the goal. There may be many different ways. Never confuse the means with the goal.

    I'd be looking at how to tell the email program to put the contents of a file as an email. In other words, I'd be looking at the scripting dictionary of your email program, and searching for examples of how to send a text file as an email in that app. Example search terms:
    applescript email body

    There might be a very simple way to do this in your email program. I don't know, because you didn't say what email program you're using.

    There will also be other routes entirely, such as reading the text file into the clipboard using the 'pbcopy' command, and then pasting the resulting clipboard as the contents of an email.

    Apple's online man page for pbcopy is currently down. You can google search terms:
    mac man page pbcopy

    Or in Terminal, paste this:
    man pbcopy
    Press space bar to advance to the next page.
    Press Q to stop showing the man page.
  5. Aouttier thread starter macrumors newbie

    Mar 3, 2016
    I use Apple Mail on OSX El Capitan. I found already some useful commands, but not the right one.
  6. Aouttier thread starter macrumors newbie

    Mar 3, 2016
    I have looked around the internet, I didn't find other ways than to read a txt file into the clipboard and to create a new message with the contents. Is this the way you meant?
    But, then I still need the read commando...
  7. chown33, May 1, 2016
    Last edited: May 1, 2016

    chown33 macrumors 604

    Aug 9, 2009
    How long is the text you want to email? You said earlier that it was too long, but you never said how long that was.

    Details are important.

    Have you looked at Automator? It has many mail commands. "New Mail Message" will take a text as input and create a new mail with the text as the email's contents.

    One way to convert a filename input into its text contents is a Run Shell Script command that contains this:
    cat "$@"
    The "pass input" popup should be set to "as arguments".

    Suppose I, chown33, wanted to replicate what you're seeing. So I want to see the error in AppleScript, see the too-big file, and so on. In short, I want to try doing this on my machines.

    The problem is, I can't try it. You haven't provided enough information.

    My earlier reply was one example. I tried what you posted. It worked. It didn't fail like yours did. Since mine worked, then at that time, I didn't know why yours was failing. I thought of several possible reasons.

    I briefly posted my results, and gave details (size of file, etc.). You then confirmed the problem was file size, but you didn't post what size caused the problem.

    If the file is too big for AppleScript's read command, but you absolutely must send it in the contents of an email (rather than an attachment), then we need to know what size that is. No one can try making a solution without that information. The file's size is one of the requirements.


Share This Page