Hello!
SetDefApp : Sets the default app for a file extension, in either the current directory, or the full subtree.
Handle with care! You can use either appname, the creator code. or the bundle id to specify the app.
Syntax:
SetDefApp -e html -a R*ch
Sets the default app of all html files in current dir to BBEdit. (Observe: the * is escaped.
SetDefApp -R -e html -a sfri
Set the default app of all html files in subtree to Safari.
SetDefApp -a Smile System\ Events.applescript Safari.applescript
Set the default app of two worksheet applescript files in current directory to Smile.
[code]#! /bin/bash
#! /bin/bash
© 2012 McUsr and put into Public Domain. You may not post this script elsewhere,
nor put into a repository but please refer to this link:
declare prgname version Usage args subtrees theExt theApp tempfoo TMPFILE curdir the_res
prgname=basename $0
PATH=“/bin:/usr/bin”
version=“0”
Usage="Usage : $prgname -h || (-R) [-a application] [-e ext] || [ file1 file2 file3 … ] "
subtrees=0
phelp()
{
echo "
$Usage
Sets the default app, for 1 or more files. globs are assumed if extensions are given.
You can’t specify patterns with globs. You must also keep the .DS_Store files to
preserve the default apps set.
Example:
$prgname -e html -a R*ch
sets the default app of all html files in current dir to BBEdit.
$prgname -R -e html -a sfri
set the default app of all html files in subtree to Safari.
$prgname -a sfri valid.html ‘Programming Mac Os X with Cocoa.html’ other\ fun\ stuff.html
set the default app of all the listed html files in current directory to Safari.
" 1>&2
}
testing for no arguments at all.
if [ $# -eq 0 ] ; then
phelp
echo “Missing arguments”
exit 1
fi
args=getopt Re:a: $* 2>/dev/null
if [ $? != 0 ]
then
phelp
exit 2
fi
setting default values
theExt=“” ; subtrees=NO ; gotExt=NO
set – $args
for i
do
case “$i”
in
-h)
phelp ;
exit 0 ;;
-R)
subtrees=YES;
shift;;
-e)
theExt=“$2”; shift;
shift; gotExt=YES;;
-a)
theApp=“$2”; shift;
shift;;
–)
shift; break;;
esac
done
tempfoo=basename $0
TMPFILE=mktemp -q /tmp/${tempfoo}.XXXXXX
if [ $? -ne 0 ]; then
echo “$prgname: Can’t create temp file, exiting…” >2
exit 1
fi
if [ ! $theApp ] ; then
phelp
echo “-a parameter is mandatory.”
exit 1
elif [ $# -eq 0 -a “$theExt” = “” ] ; then
phelp
echo “Missing file arguments.”
exit 1
elif [ ! $theExt ] ; then
echo “NO extensions”
# we collect a newline separated list of files we can get pass over to the osa-script
theExt=$1
shift
for i in “$@” ; do theExt=$theExt"
"$i ; done
echo “tmp file = “$TMPFILE
echo $theExt >”$TMPFILE”
# we have to consider this in the osascript, as we have to adjust the parameters.
fi
hvis vi bare har en fil, så kjører vi igjennom og finner de forskjellige.
curdir=pwd
/
if [ “$subtrees” == “YES” ] ; then
find . ( -name *.$theExt -and -type f ) -exec echo $curdir{} ; >$TMPFILE 2>/dev/null
elif [ “$gotExt” == “YES” ] ; then
find . ( -name *.$theExt -and -type f ) -depth 1 -exec echo $curdir{} ; 2>/dev/null >$TMPFILE
fi
the_res=$(osascript <<END! - "$TMPFILE" "$theApp"
on run {thePxFilename, theAppName}
{pxCurrentDir, hasFileExt, theFiles, theApp, subtrees}
set theFileName to POSIX file thePxFilename as alias
set {appPath, failed} to {missing value, false}
try
set appPath to path to application theAppName
end try
if appPath is missing value then
try
set appPath to path to application id theAppName
on error
set failed to true
end try
end if
if failed then return "Can't find app " & theAppName
try
set fref to open for access theFileName
on error
return "Problem with opening tempfile " & thepxfile
end try
try
set theFiles to paragraphs 1 thru -2 of (read fref as «class utf8»)
on error
return "Problem with reading tempfile " & thepxfile
end try
tell application id "com.apple.systemevents"
repeat with afile in theFiles
set default application of file ((POSIX file (contents of afile)) as alias as text) to appPath
end repeat
end tell
return "Done!"
end run
END!
)
if [ ! “$the_res” = “Done!” ] ; then
exit 1
fi[/code]