Newbie seeks help writing text files to Mac OS X

Deploying to Mac OS? Ask Mac OS specific questions here.

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
Bill Rose
Posts: 6
Joined: Tue Apr 07, 2015 3:58 pm

Newbie seeks help writing text files to Mac OS X

Post by Bill Rose » Fri Jul 29, 2016 2:46 pm

Hello all...my first post to a LiveCode forum. I have developed a little app that reads in one big text file, replaces unwanted text and formatting, and then writes out a set of smaller, cleaner text files. It works perfectly in source, merrily reading and writing files to its heart's content. :D Yesterday was the first time I built a standalone. Never writes a file. :(

I looked around the forums and found some semi-guidance for somewhat similar situations, but every combination of settings on the Mac tab failed to change anything. I feel like I must be missing something in the PLIST and/or build settings.

For what it is worth, I am running OS X (10.10.5) on a MacBook Pro. Does anyone know of common causes for me to start looking at first?

Thanks,
Bill

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7647
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Newbie seeks help writing text files to Mac OS X

Post by FourthWorld » Fri Jul 29, 2016 3:02 pm

Bill Rose wrote:Hello all...my first post to a LiveCode forum.
Welcome aboard!
I have developed a little app that reads in one big text file, replaces unwanted text and formatting, and then writes out a set of smaller, cleaner text files. It works perfectly in source, merrily reading and writing files to its heart's content. :D Yesterday was the first time I built a standalone. Never writes a file. :(
When you encounter an issue with a specific part of your code it's often helpful to include the code in your post so we can offer suggestions. For now, in the absence of the code, I can at least guess about how we might be able to diagnose this.

There are two frequently-used ways to write a file, "put <data>" into url..." and "open file..."/"write <data> to file..."/"close file...". Each uses slightly different routines internally, but both have one thing in common: you can check "the result" immediately after the presumed point of failure to see if the engine is providing error info for you. Additionally, if it is you can also include a call to the sysError() function to find out what error code the OS may be returning to provide more specifics.

For example, if you're using the "url" form, you can check "the result" like this:

Code: Select all

put tData into url ("binfile:"& tFilePath)
if the result is not empty then
    answer the result &" ("&sysError() &")"
   exit to top
end if
If you're using the open/write/close method, put that "if" block above immediately after the "open file..." statement.

If the case was an invalid path, the answer dialog may include something like this:
"can't open file (2)"

Please let us know what you find. In most cases we see either invalid paths or inappropriate write permissions, and hopefully what you're encountering will be as easily solvable.
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

Bill Rose
Posts: 6
Joined: Tue Apr 07, 2015 3:58 pm

Re: Newbie seeks help writing text files to Mac OS X

Post by Bill Rose » Fri Jul 29, 2016 5:36 pm

OK, I did get a "can't create that directory (17)" error message back when attempting to create my folder:

Code: Select all

global gMyFilePath1

on mouseUp
   set the defaultFolder to specialFolderPath("resources")
   put defaultFolder & "/" & "Files to Verify" into gMyFilePath1
     create folder gMyFilePath1
   if the result is not empty then
      answer the result &" ("&sysError() &")"
      exit to top
   end if
end mouseUp
As for the text file write outs, I use the open/write/close methods:

Code: Select all

 open file gMyFilePath1 & "/"& (item i of gTextFileNamesList)&".txt" for write
      write gSelectedPortion to file gMyFilePath1 & "/"& (item i of gTextFileNamesList)&".txt"
      close file gMyFilePath1 & "/"& (item i of gTextFileNamesList)&".txt"
Bill

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 4353
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Newbie seeks help writing text files to Mac OS X

Post by richmond62 » Fri Jul 29, 2016 5:44 pm

I use a rather simpler way of doing things:

on mouseUp
ask file "Choose where you wish to export your text"
if the result = "cancel"
then exit mouseUp
else
put the Text of fld "fStuffToExport" into url("file:" & it & ".txt")
get the longFilePath of it
set the itemDelimiter to slash
set the defaultFolder to item 1 to -2 of the longFilePath of it
end if
end mouseUp

Give it a try; you never know.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7647
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Newbie seeks help writing text files to Mac OS X

Post by FourthWorld » Fri Jul 29, 2016 6:36 pm

Thanks, Bill. I'm having a tough time searching Google to find out what error 17 means on Mac. Can you share the full path you're attempting to write to?
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

AxWald
Posts: 421
Joined: Thu Mar 06, 2014 2:57 pm

Re: Newbie seeks help writing text files to Mac OS X

Post by AxWald » Fri Jul 29, 2016 6:36 pm

Hi,

you tried another folder but "resources" already?
Maybe "temporary", "home" or "documents"?

Have fun!
Livecode programming until the cat hits the fan ...

Bill Rose
Posts: 6
Joined: Tue Apr 07, 2015 3:58 pm

Re: Newbie seeks help writing text files to Mac OS X

Post by Bill Rose » Fri Jul 29, 2016 6:47 pm

Richard,

I was having the same problem trying to find error 17. These are the two paths I am trying to write to:

/Users/bmr1/Desktop/LiveCode Jeopardy Engine/Files to Verify
/Users/bmr1/Desktop/LiveCode Jeopardy Engine/Array Files

Bill

Klaus
Posts: 11730
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Newbie seeks help writing text files to Mac OS X

Post by Klaus » Fri Jul 29, 2016 6:54 pm

Hi Bill,

1. welcome to the forum! :D

2.
OK, I did get a "can't create that directory (17)" error message back when attempting to create my folder:

Code: Select all

global gMyFilePath1
on mouseUp
set the defaultFolder to specialFolderPath("resources")
...
In a standalone this folder is inside of the MacOS APP packed and a "normal" user does not have WRITE permissions here,
that's why you get hte above mentioned error!

if you need to store any app data on a Mac use either:
-> specialfolderpath("documents")
or maybe:
-> specialfolderpath("preferences")

Check the dictionary for more (and platform specific) specialfolderpath names.

Code: Select all

...
set the defaultFolder to specialFolderPath("resources")
put defaultFolder & "/" & "Files to Verify" into gMyFilePath1
create folder gMyFilePath1
...
Avoid setting the defaultfolder if you can have an bulletproof absolute pathname :D
AND ALWAYS do some errorchecking!
Yes, it's tedious, but neccessary!

Code: Select all

...
put specialFolderPath("resources") & "/Files to Verify" into gMyFilePath1
if there is not a folder gMyFilePath1 then
   create folder gMyFilePath1
end if
...
Get used to put parens when concatenating object and file names to avoid surpises from the engine!

Code: Select all

...
open file (gMyFilePath1 & "/"& (item i of gTextFileNamesList)&".txt") for write
write gSelectedPortion to file (gMyFilePath1 & "/"& (item i of gTextFileNamesList)&".txt")
close file (gMyFilePath1 & "/"& (item i of gTextFileNamesList)&".txt")
...
Or save some typing by putting the filepath into a variable first :D

Code: Select all

...
put gMyFilePath1 & "/"& (item i of gTextFileNamesList)&".txt" into tTargetFile
open file tTargetFile for write
write gSelectedPortion to file tTargetFile
close file tTargetFile
...
If you deal with smallish (maybe max 20 MB) data, you can use the shorter URL syntax if you like

Code: Select all

...
put gSelectedPortion  into URL("FILE:" & gMyFilePath1 & "/"& (item i of gTextFileNamesList)&".txt")
## Supposed the content of gSelectedPortion is TEXT, if it is binary stuff, use BINFILE
...

Best

Klaus

Bill Rose
Posts: 6
Joined: Tue Apr 07, 2015 3:58 pm

Re: Newbie seeks help writing text files to Mac OS X

Post by Bill Rose » Fri Jul 29, 2016 7:53 pm

Klaus,

Bingo! The problem was indeed my pointing to the resources path, which also further explains the odd behavior of it working fine prior to packing it up, but never again afterwards. Lesson learned.

I ditched the default Folder setting I was doing and went with this:

Code: Select all

   put specialFolderPath("documents") & "/" & "Files to Verify" into gMyFilePath1
I am just too green , but it is coming. I will implement your other tips also. Many thanks Klaus, Richard, and others.

Post Reply

Return to “Mac OS”