Go Back   The macosxhints Forums > OS X Help Requests > AppleScript



Reply
 
Thread Tools Rate Thread Display Modes
Old 05-22-2010, 12:53 PM   #1
MinusNick
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
The problem is in the "save first document as counterstring..." line. I'm not even sure about that syntax. Can someone help me out on this?
MinusNick is offline   Reply With Quote
Old 05-22-2010, 04:33 PM   #2
MinusNick
Prospect
 
Join Date: May 2010
Posts: 5
Also, I'm using Mac OSX 10.5.8.
MinusNick is offline   Reply With Quote
Old 05-22-2010, 04:42 PM   #3
tw
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-
tw is offline   Reply With Quote
Old 05-22-2010, 05:17 PM   #4
MinusNick
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?
MinusNick is offline   Reply With Quote
Old 05-22-2010, 06:51 PM   #5
tw
Hall of Famer
 
Join Date: Apr 2007
Posts: 4,262
Quote:
Originally Posted by MinusNick
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?

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
If you're just learning AppleScript, google for the AppleScript Language Guide and download it (find a copy on the one of the Apple websites so you know you're getting a current revision). Lots of good stuff in that.
__________________
Philosophy is a battle against the bewitchment of our intelligence by means of language. -LW-
tw is offline   Reply With Quote
Old 05-22-2010, 07:02 PM   #6
MinusNick
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.
MinusNick is offline   Reply With Quote
Old 05-22-2010, 07:29 PM   #7
hayne
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
hayne is offline   Reply With Quote
Old 05-23-2010, 02:10 AM   #8
tw
Hall of Famer
 
Join Date: Apr 2007
Posts: 4,262
Quote:
Originally Posted by hayne
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.

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-
tw is offline   Reply With Quote
Old 05-23-2010, 08:42 AM   #9
MinusNick
Prospect
 
Join Date: May 2010
Posts: 5
Card catalogs... lol. Thanks a lot for the extra info!
MinusNick is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 08:46 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.