|
|
#1 |
|
Prospect
Join Date: May 2010
Posts: 5
|
Creating multple txt files, saving them to folder
Hello everyone. I want to get into Applescript, and I've been pouring over the Internet for hours. I'm making a script that will create many text files and save them to a folder on the desktop; However, I cannot get one thing to work correctly... it says access to that folder is not allowed. I checked the privileges and set them all to read and write, but it's still no good. Here is my script:
Code:
set this_folder to (path to desktop)
tell application "Finder"
if (exists folder "AAA" of this_folder) is false then
make new folder at this_folder with properties {name:"AAA"}
end if
end tell
tell application "TextEdit"
set counter to 1
repeat 13 times
set counterstring to counter as string
make new document
set text of first document to counterstring
save first document as counterstring in "AAA" of this_folder
set counter to counter + 1
end repeat
close
end tell
|
|
|
|
|
|
#2 |
|
Prospect
Join Date: May 2010
Posts: 5
|
Also, I'm using Mac OSX 10.5.8.
|
|
|
|
|
|
#3 |
|
Hall of Famer
Join Date: Apr 2007
Posts: 4,262
|
there were a number of problems with this script. look at this version to see what the differences are:
Code:
set this_folder to (path to desktop)
tell application "Finder"
if (exists folder "AAA" of this_folder) is false then
make new folder at this_folder with properties {name:"AAA"}
end if
end tell
tell application "TextEdit"
repeat with i from 1 to 13
make new document
set text of first document to (i as text)
save first document in file ((this_folder as text) & "AAA:new text file " & i & ".txt")
end repeat
close
end tell
__________________
Philosophy is a battle against the bewitchment of our intelligence by means of language. -LW- |
|
|
|
|
|
#4 |
|
Prospect
Join Date: May 2010
Posts: 5
|
Ah, I didn't know repeat loops could be structured like that. It reminds me of for loops in Actionscript. And thanks a ton for fixing that up for me. I didn't know I had to use a colon.
I have another question: when I run this script, how can I make it so it doesn't open up all those windows in TextEdit? I try putting "quit TextEdit" at the end, but it does the whole "save changes?" prompt. Can I have a command that quits it and says Discard Changes? Is there a way to completely bypass opening up 13 windows? |
|
|
|
|
|
#5 | |||||||||||||||||||||||
|
Hall of Famer
Join Date: Apr 2007
Posts: 4,262
|
colons are the path delimiters in Mac-style file specifications (i.e. "...AAA:file" is the Mac way of saying that 'file' is inside folder 'AAA' there's no way to not open these windows in TextEdit if you're using TextEdit to make the files. however, you can make text files without using TextEdit. that would look like this: Code:
set this_folder to (path to desktop)
tell application "Finder"
if (exists folder "AAA" of this_folder) is false then
make new folder at this_folder with properties {name:"AAA"}
end if
end tell
repeat with i from 1 to 13
set theText to "This is the Text that will go in file # " & i
set theFile to ((this_folder as text) & "AAA:new text file " & i & ".txt")
set fp to open for access theFile with write permission
write theText to fp
close access fp
end repeat
__________________
Philosophy is a battle against the bewitchment of our intelligence by means of language. -LW- |
|||||||||||||||||||||||
|
|
|
|
|
#6 |
|
Prospect
Join Date: May 2010
Posts: 5
|
Thanks a lot, now I know about write commands! And I'll read that guide.
Also, I've found something interesting... You can have a 0-byte file if there is no text in the file. It truly is 0-bytes because three online uploaders have confirmed it. When I put one character in the text file, it is 1 byte, and thus takes up the 4-kb block. But a folder full of 500 of these 0-byte files is still 0 bytes... This is a bit off topic, but do you know where the information for the name of the text file goes? Even a file with a long name and no content is 0 bytes. |
|
|
|
|
|
#7 |
|
Site Admin
Join Date: Jan 2002
Location: Montreal
Posts: 31,956
|
The filename and other "metadata" for the file is stored in bookkeeping structures in the filesystem. It's sort of like a library where none of the books have any identifying info on the cover but the card catalog keeps track of where each book is and what its name is.
__________________
hayne.net/macosx.html |
|
|
|
|
|
#8 | |||||||||||||||||||||||
|
Hall of Famer
Join Date: Apr 2007
Posts: 4,262
|
wow, you know, I haven't seen a card catalog in a good 15 years. I usually tell students it's like GPS - you can have a list of GPS coordinates telling you where everything is without expecting to see the GPS coordinates written on everything you're looking for (e.g., the Musee Toulouse-Lautrec in Albi, France, is at 43.928685(lat) 2.143878(long) 176(alt), numbers unlikely to appear on the museum's facade). so, you have a file directory telling you that a file named "A" is located at position X on the disk and has such-and-such properties, but position X just contains file data. If I remember correctly, there is a maximum size to the number of files you can have - something to do with the structure of the HFS directories, not with available disk space - so you can't create arbitrarily many zero-byte files.
__________________
Philosophy is a battle against the bewitchment of our intelligence by means of language. -LW- |
|||||||||||||||||||||||
|
|
|
|
|
#9 |
|
Prospect
Join Date: May 2010
Posts: 5
|
Card catalogs... lol. Thanks a lot for the extra info!
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|