Tuesday, December 12, 2017

#1 2009-09-20 12:09:12 pm

Richard
Member
From:: Herefordshire, UK
Registered: 2009-09-09
Posts: 529
Website

Preferences and Resources

I have an AS .txt text editor (all on my public iDisk imadrichard in the scripts folder named Richard's Text Editor 3) which has a dialog text entry, which I want to be a large text field in ASOC.

ATM I have this around the start:

Applescript:

set val1 to (read file ((path to resource "Save Location Pref.txt") as string)) --get contents of of file, which will be written as a path as string to be made an alias later because when distributed it is set to "Default"

And these at the end:

Applescript:

open for access (file ((path to resource "Save Location Pref.txt") as string)) with write permission
   set eof (file ((path to resource "Save Location Pref.txt") as string)) to 0
   write (val3 as string) to (file ((path to resource "Save Location Pref.txt") as string)) as text
   close access (file ((path to resource "Save Location Pref.txt") as string))

(Would you believe all those brackets are needed! lol)

What is the equivalent resource method in ASOC?

Also is it easier to use .plist ?

Offline

 

#2 2009-09-20 04:52:53 pm

Craig Williams
Administrator
From:: Ft. Smith, AR
Registered: 2006-12-07
Posts: 888

Re: Preferences and Resources

Here is a way to read a text file inside your bundle. Writing to it is very similar.
Look at NSString in the docs for writeToFile method.

Applescript:

   on buttonClick_(sender)
       tell current application's class "NSBundle"
           tell its mainBundle()
               set textFile to its pathForResource_ofType_("sample", "txt")
           end tell
       end tell
       
       tell class "NSString" of current application
           -- NSUTF8StringEncoding has a value of 4
           set textFileContents to its stringWithContentsOfFile_encoding_error_(textFile, 4, missing value)
       end tell
       textView's setString_(textFileContents)
   end buttonClick_


Filed under: NSString

Offline

 

#3 2009-09-20 06:00:03 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5199

Re: Preferences and Resources

It's probably worth pointing out that you can still use your AS read/write commands if you wish.

But I wonder if what you should really be looking at is using user defaults instead.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#4 2009-09-20 06:32:02 pm

Richard
Member
From:: Herefordshire, UK
Registered: 2009-09-09
Posts: 529
Website

Re: Preferences and Resources

I would just like the least ObjC like (unless it compromises things) way to do what my script already does. However I will need a write command for writing to the edit file too. OK, I think at this point it's easiest to just paste the whole thing for people to see.

Applescript:

display dialog "Open last saved or choose file" buttons {"Open last", "Open other"} default button 2
if button returned of result is "Open last" then
   set val1 to (read file ((path to resource "Save Location Pref.txt") as string))
   --Read pref file to find out path of last edited file.
   if val1 is "Default" then
       EditFile("no")
   else
       EditFile(val1 as alias)
   end if
else
   EditFile("no")
end if

on EditFile(val2)
   if val2 is "no" then
       set val3 to (choose file with prompt "Please select a .txt file:

To save a text file as .txt in TextEdit, click 'Make Plain Text' in the Format menu and save the file."
)
       --Get file path.
   else
       set val3 to val2
   end if
   if (get eof val3) is 0 then --Read doesn't like eof (end of file) to be 0 bytes.
       set val4 to ""
   else
       set val4 to read val3 --Read file before it gets erased.
   end if
   open for access val3 with write permission --Open file.
   set eof val3 to 0 --Erase file.
   repeat
       display dialog "Edit your text." default answer val4 buttons {"Revert", "Save"}
       set {val5, val6} to {text returned of result, button returned of result}
       if val6 is "Save" then
           write val5 to val3
           exit repeat
       end if
   end repeat
   --Write to file giving old data as default (to give the same affect as as read and edit).
   close access val3
   open for access (file ((path to resource "Save Location Pref.txt") as string)) with write permission
   set eof (file ((path to resource "Save Location Pref.txt") as string)) to 0
   write (val3 as string) to (file ((path to resource "Save Location Pref.txt") as string)) as text
   close access (file ((path to resource "Save Location Pref.txt") as string))
   --Save path of edited file to pref file.
end EditFile

Last edited by Richard (2009-09-20 06:53:41 pm)

Offline

 

#5 2009-09-20 09:00:57 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5199

Re: Preferences and Resources

Richard wrote:

I would just like the least ObjC like (unless it compromises things) way to do what my script already does.


You probably only need to use pathForResource_ofType_ instead of "path to resource", as in Craig's post, and put the read/write stuff inside a "tell current application" block.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#6 2009-09-21 10:43:28 am

Richard
Member
From:: Herefordshire, UK
Registered: 2009-09-09
Posts: 529
Website

Re: Preferences and Resources

Is this another ASOC thing? Do I have to put many standard commands inside tell current app?

Offline

 

#7 2009-09-21 11:51:59 am

Richard
Member
From:: Herefordshire, UK
Registered: 2009-09-09
Posts: 529
Website

Re: Preferences and Resources

And what are the parameters for the pathForResource_ofType_ command?

smile... sad I'm getting quite confused

Last edited by Richard (2009-09-21 05:15:48 pm)

Offline

 

#8 2009-09-21 06:06:02 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5199

Re: Preferences and Resources

Richard wrote:

Is this another ASOC thing? Do I have to put many standard commands inside tell current app?


I've only found it with the read/write commands so far.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#9 2009-09-21 06:09:20 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5199

Re: Preferences and Resources

Richard wrote:

And what are the parameters for the pathForResource_ofType_ command?


Get used to using Xcode's Help -> Developer Documentation. Search for pathToResource:ofType:, and you'll see it requires the name of the file and the extension. So in your case: "Save Location Pref", "txt"


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#10 2009-09-22 07:32:15 am

Richard
Member
From:: Herefordshire, UK
Registered: 2009-09-09
Posts: 529
Website

Re: Preferences and Resources

OK but search doesn't work

Offline

 

#11 2009-09-22 08:02:05 am

Richard
Member
From:: Herefordshire, UK
Registered: 2009-09-09
Posts: 529
Website

Re: Preferences and Resources

OK reeaalllyyy weird thing just happened. I tried out of interest to put a blank text file in the resources folder of the project and run basically a copy of my AppleScript. I do, it should crash after clicking open last but it actually finds the last used file from the bundle version, it even keeps working on build and run without the text file in the resources folder. I did copy and paste the whole script from AS Editor and insert the awakeFromNib bits so did the path to resource command remember the path to the bundle or something?

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)