How do I use a shell script to generate Text File?

my shell script experience is limited to using sed and the occasional grep.

I am looking to generate a text file from scratch to a chosen directory.

I have a bunch of variables with button coordinate data that I want to put into a text file

the pseudo code is something like this:

set layerTotal to number of layers from from photoshop
repeat with i from 1 to count of layerTotal
set buttonInfo to layer bounds of layer i
add buttonInfo to text file
end repeat

at the end I want a text file named “Composition X.txt”

and inside something like:

Button 1{271,71,391,89}
Button 2{271,171,391,189}
Button 3{271,271,391,289}
Button 4{271,371,391,389}

the only part I don’t know how to do is creating a text file and then appending it each time through repeat.

I am pretty sure the cat shell command is needed but i don’t know the proper syntax.

i appreciate the help,

ryan

This should work for you, but I’m curious why you want to do it from the shell in the first place?

set filePath to quoted form of (POSIX path of (path to desktop) & "Composition X.txt")
tell application "Adobe Photoshop CS3"
	set theDoc to front document
	repeat with i from 1 to (count layers of theDoc)
		set theBounds to bounds of layer i of theDoc
		tell theBounds
			set theCommand to "echo \"Button " & i & "{" & item 1 & "," & item 2 & "," & item 3 & "," & item 4 & "}\" >> " & filePath
		end tell
		do shell script theCommand
	end repeat
end tell

that works great!!
thank you.

the reason I wanted to do shell commands is because in my experience they operate lighting fast and aren’t dependent on opening up any other applications.

Do you think there is a better method?

thanks again,

ryan

sorry, another question.

is there a way to set it up so that if the file path already exists when the command is being run, it completely overwrites the contents instead of appending what is already there?

thanks

The shell commands invoked operate lighting fast, but there’s quite an overhead involved in using do shell script to invoke them. It sets up its own shell, runs the commands within it, then ditches the shell. So for single or simple operations, a shell script can often be much slower than vanilla AppleScript or other OSAX commands. For your purposes, the File Read/Write commands would be faster, though a bit more complicated to use.

Probably with a shell script. Definitely with File Read/Write. I’ve taken James’s Photoshop script at face value here, since I don’t have that particular application. Most of the running time will involve Photoshop itself, so you may not actually notice the improvement in performance. :wink:

set LF to (ASCII character 10)
-- Open a write permission access to the text file. (The file will be created if it doesn't already exist.)
set fRef to (open for access file ((path to desktop as Unicode text) & "Composition X.txt") with write permission)
-- Put everything in a try block, so that the script will survive any errors and close the access at the end.
try
	set eof fRef to 0 -- Lose any current file contents.
	tell application "Adobe Photoshop CS3"
		set theDoc to front document
		repeat with i from 1 to (count layers of theDoc)
			set theBounds to bounds of layer i of theDoc
			tell theBounds
				set theLine to "Button " & i & "{" & item 1 & "," & item 2 & "," & item 3 & "," & item 4 & "}" & LF
			end tell
			-- The script, not Photoshop, opened the access, so it must do the writing.
			tell me to write theLine as string to fRef starting at eof
		end repeat
	end tell
end try
close access fRef