Go Back   The macosxhints Forums > OS X Help Requests > UNIX - General



Reply
 
Thread Tools Rating: Thread Rating: 3 votes, 5.00 average. Display Modes
Old 10-30-2007, 08:19 AM   #1
Nonsense
Registered User
 
Join Date: Oct 2007
Posts: 2
Write ls result to txt-file?

I'm a bit new at this, but I'll try to explain what I want to do.

I'd like to be able to use a Applescript that lists the contents of a directory and then embed the result in a text-file called 'files.txt' in the same directory.
Then I'd like the script to replace all ".jpg" with "" in the the text file.


Code:
tell application "Terminal"
	do shell script "ls -1 /Volumes/name/directory/"
end tell
Then what?
Nonsense is offline   Reply With Quote
Old 10-30-2007, 08:56 AM   #2
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 32,459
The easiest thing to do would be to redirect the shell command output using '> files.txt' in the command. See the section on redirecting output in this Unix FAQ: http://forums.macosxhints.com/showthread.php?t=40648
__________________
hayne.net/macosx.html
hayne is offline   Reply With Quote
Old 10-30-2007, 09:00 AM   #3
tlarkin
League Commissioner
 
Join Date: Mar 2003
Location: Bay Area, CA
Posts: 11,352
you could try this

Code:
ls -al /Volumes | open -f -t
tlarkin is offline   Reply With Quote
Old 10-30-2007, 09:15 AM   #4
bramley
MVP
 
Join Date: Apr 2004
Location: Cumbria, UK
Posts: 2,461
Code:
tell application "Terminal"
	do shell script "ls -1 /Volumes/name/directory/"
end tell
Note when using 'do shell script' it is unnecessary (indeed it is an error) to invoke Terminal.app.

The following will perform the same function without using Terminal
Code:
do shell script "ls -1 /Volumes/name/directory/"
The correct command to use with Terminal is 'do script'
bramley is offline   Reply With Quote
Old 10-30-2007, 09:18 AM   #5
brettgrant99
All Star
 
Join Date: May 2007
Posts: 674
How about:

ls -1 | awk -F\. '{if($NF ~ /jpg/) {for(i=1;i<NF-1;i++) printf("%s.",$i); printf("%s\n",$(NF-1))} else print $0}' > fileName

which should do it all in one swoop.

If you know the directory only contains .jpg files you could also do this in a tcsh:

touch fileName
foreach f(*)
echo $f:r >> fileName
end

I don't do bash, so I don't know if it has similar builtins. The >> appends to a file, so watch out for duplicate lines. Likewise, the touch command is only needed if fileName doesn't already exist.

Both of those you would need to modify if you wanted the full path info in the file.

HTH,
Brett
brettgrant99 is offline   Reply With Quote
Old 10-30-2007, 01:56 PM   #6
tw
Hall of Famer
 
Join Date: Apr 2007
Posts: 4,262
in applescript (without shell scripting) you'd do it this way

Code:
set theFolder to choose folder with prompt "Choose a folder to process:"
tell application "Finder"
	set extension hidden of every file of theFolder to true
	set theList to displayed name of every file of theFolder whose name extension is "jpg"
	set extension hidden of every file of theFolder to false
end tell
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
set theText to theList as text
set AppleScript's text item delimiters to tid
set newFileName to (theFolder as text) & "File List.txt"
set fp to open for access file newFileName with write permission
write theText to fp
close access fp
I'm assuming that you only want the jpg files listed, and not any of the other files...
tw is offline   Reply With Quote
Old 10-30-2007, 04:00 PM   #7
P6SMSKC
Triple-A Player
 
Join Date: Aug 2007
Location: Sierra Mtns.
Posts: 78
as a bash script...

Code:
#!/bin/bash

FOUND=0
touch jpg_list.txt


for filename in *
do
	echo "$filename" | grep -q .jpg
	if [ $? -eq $FOUND ]
	then
		echo ${filename%.jpg} >> jpg_list.txt
	fi
done
I'm sure this is ugly to actual un*x people, as I'm a hack (opposite of hacker), but it does the trick.

I'm sure it could be written as a single line of code like brett has done, but to me that obscures the process, and you (and I) are trying to learn the process.

BTW, brett's one-liner doesn't work for me... I get a list of every file in the directory, with .jpg removed from the files with that extension. close.

I've considered learning applescript, but have decided that whenever possible I'll use shell scripting, as this will carryover to many more systems. Not to mention it's ridiculously powerful (and confusing).. good luck...

josh
P6SMSKC is offline   Reply With Quote
Old 10-30-2007, 04:12 PM   #8
brettgrant99
All Star
 
Join Date: May 2007
Posts: 674
Quote:
Originally Posted by Nonsense
I'm a bit new at this, but I'll try to explain what I want to do.

...
Then I'd like the script to replace all ".jpg" with "" in the the text file.
...

I interpreted that to mean that he wanted the name without the extension, which is what my one-liner does. If you just want all of the .jpg files, just do

ls -1 *.jpg > fileName

Brett
brettgrant99 is offline   Reply With Quote
Old 10-30-2007, 04:19 PM   #9
baf
MVP
 
Join Date: Jun 2007
Location: Skellefteċ, Sweden
Posts: 1,173
In reply to #7

Remember to clear the list first

Code:
>jpg_list.txt
instead of
Code:
touch jpg_list.txt
Why test with grep ?
either do:
Code:
for filename in *.jpg
do
    echo ${filename%.jpg} >> jpg_list.txt
done
Or as I think was the intention
Code:
for filename in *
do
    echo ${filename%.jpg} >> jpg_list.txt
done
and if you want to test for .jpg do
Code:
for filename in *
do
    if [[ $filename == *.jpg ]]; then
        echo $filename
    fi
done
__________________
/Bengt-Arne Fjellner IT-Administrator Luleċ university, Sweden.
Some say: "You learn as long as you live".
My way: "You live as long as you learn".
baf is offline   Reply With Quote
Old 11-07-2007, 06:24 AM   #10
Nonsense
Registered User
 
Join Date: Oct 2007
Posts: 2
Thanks for all help.
Some interesting suggestions.
I'll try some of them and decide which is best for my needs...
Nonsense 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 12:10 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, 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.