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”
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
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.
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?
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.
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