I’ve made a pretty simple applescript application, now I need to know how to save files (text) and open them, the extension for the files are .cfg for saving and opening.
Thanks but I dont understand how I can add that into
else if title of theObject = "save" then
end if
else if title of theObject = "save" then
set thePath to (path to desktop as Unicode text) & "Aardvark.txt"
set fRef to (open for access file thePath with write permission)
try
set eof fRef to 0
write "Hello" to fRef
write " World" to fRef -- Continues from the file mark after the previous write.
end try
close access fRef
read file thePath
--> "Hello World"
end if
Doesnt seem to work.
When I make a sperate applescript in script editor it works fine. Why doesn’t in work in XCODE?
Do you get an error message or does nothing happen when you press the button?
If nothing happens when you click the button it just may be that you are referencing it wrong. Is the title of the button “save” or is the name of the button “save” because they are two different things.
OMG TY.
else if name of theObject = "save" then
set thePath to (path to desktop as Unicode text) & "Myscript.cfg"
set fRef to (open for access file thePath with write permission)
try
set eof fRef to 0
write "Hello World" to fRef
-- Initialise the file mark to the 7th byte by writing nothing, starting there.
write "" to fRef starting at 7
-- Successive writes continue from there..
write "everyone" to fRef
write ", everywhere." to fRef
end try
close access fRef
set theData to (read file thePath)
--> "Hello everyone, everywhere."
end if
end clicked
It works yay, thanks so much!.
But how can I get it to save what was entered into the window?
else if name of theObject = "save" then
set thePath to (path to desktop as Unicode text) & "test.cfg"
set fRef to (open for access file thePath with write permission)
try
set eof fRef to 0
set the contents of text view 1 of scroll view 1 of window 1 to thisText
write "test.cfg" to fRef
end try
Does not work :(.
Try this
if name of theObject = "save" then
try
set thePath to (path to desktop as Unicode text) & "test.cfg"
set fRef to (open for access file thePath with write permission)
set eof fRef to 0
set thisText to contents of text view 1 of scroll view 1 of window 1
write "test.cfg" & return to fRef
write thisText to fRef
close access fRef
on error err
log err
close access fRef
end try
end if
Thanks!, Could you give an example of opening a file?
Hi JDogg,
Maybe the following code gets you started? Please note, that (text) files can feature different encodings (UTF-16, UTF-8, Windows Latin 1, ASCII), so you might need additional code to handle this circumstance.
set fcont to missing value
set thePath to (path to desktop as Unicode text) & "test.cfg"
-- does the file exist?
try
set theAlias to thePath as alias
on error
-- no, it does not
return fcont
end try
-- trying to open and read the existing file
try
set fRef to (open for access file thePath)
set fcont to read fRef
close access fRef
on error err
log err
try
close access fRef
end try
end try
-- returning the file content
return fcont
Thanks!, is there any way to let the user choose what file to open?
set filepath to (choose file with prompt "Please choose a file to open:" without multiple selections allowed, showing package contents and invisibles) as Unicode text
HTH
If you want to make it a bit fancier you can use open and save panels. This way you limit what files can be opened and you can also set the file type for the file you want to save.
on clicked theObject
if name of theObject = "save" then
tell save panel
set title to "Save File"
set required file type to "cfg" --This desgnates an extension of .cfg for the files
set treat packages as directories to true
end tell
set theresult to (display save panel)
if theresult = 1 then
try
set saveFile to (POSIX file (path name of save panel)) as string
set openSavefile to open for access saveFile with write permission
set eof of openSavefile to 0
set thisText to contents of text view 1 of scroll view 1 of window 1
write thisText & return to openSavefile
close access openSavefile
on error err
log err
close access openSavefile
end try
end if
end if
if name of theObject is "Open" then
tell open panel
set title to "Open File"
set treat packages as directories to true
set can choose directories to false
set can choose files to true
set allows multiple selection to false
end tell
--The file type has to be a list.
set theFileTypes to {"cfg"} --This means that you can only open .cfg files
set theresult to (display open panel for file types theFileTypes)
if theresult = 1 then
set theFile to (POSIX file (path name of open panel)) as alias
try
set readTheFile to read theFile
set contents of text view 1 of scroll view 1 of window 1 to readTheFile
end try
end if
end if
end clicked
Dallas
That works great!, thanks so much for your help guys. I hope to be as good as you one day