Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Open a random file in the GUI via a bash script UNIX
Here's my scenario: I have a folder with several thousand writings (.txt, .rtf, and .doc files) that span many years of my doing daily freewriting exercises. These freewritings are the basis for rough-drafts, which are the basis for short stories, etc. However, the hardest part of having so many bits and pieces is reading them over to revise them.

What I wanted was to have my Mac occasionally just open a random bit of writing I've done. With some googling and some referring to the man pages, I came up with the following. As with all bash scripts, once saving it, you have to do chmod +x scriptname. Personally, I call this from cron, so that each morning I have a random writing to read when I wake up.

Read on for the script...

#!/bin/bash

# File locations
WritingsPath=/Users/me/Documents/Writings
TempLog=/tmp/randomwriting.log

# Create a temporary logfile of all matches
find $WritingsPath -iregex ".*.txt" > $TempLog
find $WritingsPath -iregex ".*.rtf" >> $TempLog
find $WritingsPath -iregex ".*.doc" >> $TempLog

# Choose a random line number (any number from 1 to the length of the file)
LowerBound=1
RandomMax=32767
UpperBound=$(cat $TempLog | wc -l)
RandomLine=$(( $LowerBound + ($UpperBound * $RANDOM) / ($RandomMax + 1) ))

# Use sed to grab the random line
Command=$(sed -n "$RandomLine{p;q;}" "$TempLog")

# open the random line in TextEdit
open -e "$Command"
If anyone knows how to combine the find statements onto one line, I'd love to hear it, as I could then significantly shorten this script.

Basically, this bit of code calls find against a folder for each of three extensions. find returns a list with the full path of each file, which is dumped to a temporary file. This file is then counted for the number of lines (wc -l), which is used as the upper bound when choosing a random line. The randomly-chosen line number is then used by sed to get just that line from the file, which is then passed to open -e to open in TextEdit.
    •    
  • Currently 2.20 / 5
  You rated: 4 / 5 (5 votes cast)
 
[17,576 views]  

Open a random file in the GUI via a bash script | 11 comments | Create New Account
Click here to return to the 'Open a random file in the GUI via a bash script' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Open a random file in the GUI via a bash script
Authored by: barretpj on Nov 14, '05 07:22:02AM
This would work, using -o for OR:

find $WritingsPath -iregex ".*.txt" -o -iregex ".*.rtf" -o -iregex ".*.doc" > $TempLog
NB you should escape the extension dot, otherwise the regexp will interpret . as "any character", so ".*.doc" match a file called "postdoc". Better still, use -iname:

find $WritingsPath -iname "*.txt" -o -iname "*.rtf" -o -iname "*.doc" > $TempLog


[ Reply to This | # ]
Open a random file in the GUI via a bash script
Authored by: kejadlen on Nov 14, '05 07:40:54AM
In Ruby:
Dir.chdir "/Users/me/Documents/"
docs = Dir["*.{txt,rtf,doc}"]
exec "open", "-e", docs[rand(docs.size)]
Temp files are a slight security risk, and this seemed a little cleaner.

[ Reply to This | # ]
Open a random file in the GUI via a bash script
Authored by: avarame on Nov 14, '05 12:07:39PM

I laughed when I saw this post. Of COURSE there's an easier way to do it in Ruby!

Kudos to you, sir or ma'am!



[ Reply to This | # ]
Open a random file in the GUI via a bash script
Authored by: CkB_Cowboy on Nov 14, '05 08:00:04AM
Here's a much simpler solution:
#!/bin/bash

# Writings location
WritingsPath=~/Public/Downloads

# Writings extensions (delimited by the | char)
WritingsExt='txt|rtf|doc'

find -E "$WritingsPath" -iregex ".*.($WritingsExt)" |
(
	# Put all lines into lines[] array
	i=0; while read line; do lines[i++]="$line"; done
	
	# Open a random lines[] element
	open -e "${lines[$RANDOM % $i]}"
)

---
My ill-matic homepage:
http://rj3.net/cowboy/

[ Reply to This | # ]

Open a random file in the GUI via a bash script
Authored by: gshenaut on Nov 14, '05 09:25:29AM
Well, if the files are all at the same level in the folder, you could use
ls *.{doc,rtf,txt}
to get the files. You might be able to use something like
i=$(( 1 + $RANDOM % $(ls *.{rtf,txt,doc} | wc -l) ))
open `ls *.{rtf,txt.doc} | sed -e ${i}p -e d`
(I use ksh, bash may be slightly different.)

Greg Shenaut

[ Reply to This | # ]

Open a random file in the GUI via a bash script
Authored by: adrianm on Nov 14, '05 10:32:05AM
If you want to do this in applescript, cron this, or put it in your login items, or whatever:

tell application "Finder"
	open some document file in folder "DiskName:Users:me:Somefolder"
end tell
Ensures it is a document (including a .rtfd bundle-based doc).

You can narrow it with a where name ends with ".doc" clause too.

[ Reply to This | # ]

Open a random file in the GUI via a bash script
Authored by: adrianm on Nov 14, '05 10:40:22AM
Oh, and you can script it from bash/zsh etc by:

osascript -e '
...my previous script...
'


[ Reply to This | # ]
Open a random file in the GUI via a bash script
Authored by: periche on Nov 14, '05 08:58:22PM
If the folder will always be the Documents folder in the home directory of the user, you could use:

tell application "Finder"
    open some document file in (path to documents folder from user domain)
end tell

This way it is trasnportable to any mac in any laguage and will always work properly for all users on a system, always using the logged user's Document Folder.

[ Reply to This | # ]

GeekTool
Authored by: iteratix on Nov 14, '05 10:49:49AM

You know what, this is perfect for GeekTool. Use one of these scripts or a modification and have GeekTool display a random snippet of text right on your desktop! =)



[ Reply to This | # ]
Open a random file in the GUI via a bash script
Authored by: kenahoo on Nov 14, '05 06:45:38PM
And here's a perl equivalent, similar to the Ruby one:

  @files = glob("/Users/me/Documents/Writings/*");
  exec "open", "-e", $files[rand @files];
-Ken

[ Reply to This | # ]
Open a random file in the GUI via a bash script
Authored by: kenahoo on Nov 14, '05 06:59:51PM
Oops, I forgot about the restrictions on extensions:

  @files = glob("/Users/me/Documents/Writings/*.{rtf,doc,txt}");
  exec "open", "-e", $files[rand @files];
-Ken

[ Reply to This | # ]