show the uti of a file from command line.

I am lazy, so I’d rather see the uti of a file directly, than having to resort to doing something else than executing a shell script.

Save it in your bin folder, and execute chmod u+x uti , (I have just named it uti).

[code]#! /bin/bash

© McUsr 2012 and put in public domain. You may not post this elsewhere, nor put it in a public accessible repository.

Please refer to this link: http://macscripter.net/viewtopic.php?pid=156372#p156372

Returns a uti of a file

declare prgname Usage version
prgname=basename $0
PATH=“/bin:/usr/bin”
version=“0”
Usage=“Usage : $prgname file”

phelp()
{
echo "
$Usage

Returns the Apple Ox X uti of a single file; globs not allowed." 1>&2

}
if [ $# -ne 1 ] ; then
echo “Bad numbers of parameters” 1>&2
phelp
exit 1
elif [ “$1” = “-h” ] ; then
phelp
exit 0
fi

echo “$1” |grep “/” >/dev/null

check if there is a path involved here

if [ $? -eq 0 ] ; then
fname=“$1”
else
fname=pwd/“$1”
fi
if [ ! -r “$fname” ] ; then
echo “The file “$fname” does not exist!” 1>&2
phelp
exit 1
fi

osascript <<END! - “$fname”
on run {pathToafile}
local pxfile, tuti
set pxfile to POSIX file pathToafile as text

tell application id "com.apple.systemevents"
	set tuti to type identifier of disk item pxfile
end tell
tuti

end run
END![/code]

Hello. I’ll confine this suite of probably unnecessary “tools” to this thread. :slight_smile:

bid returns the bundle identifer upon receiving either a valid appname or four lettered creator code (CFBundleSignature)

I will use this with the open command (man open).

[code]#! /bin/bash

© McUsr 2012 and put in Public domain.

You may not post this elsewhere or put it into a repository

declare prgname version Usage ;
prgname=basename $0
PATH=“/bin:/usr/bin”
version=“0.01”
Usage=“Usage : $prgname appname”

phelp()
{
echo "
$Usage
Returns a bundleid if it can finds something looking like a: an appname, or b: a creator code (CFBundleSignature).
Case is sensitive." 1>&2

}
if [ $# -ne 1 ] ; then
echo “Bad numbers of parameters” 1>&2
phelp
exit 1
elif [ “$1” = “-h” ] ; then
phelp
exit 0
fi

the_res=$(osascript <<END! - “$1”
on run {anAppName}
local failed, bid
set {failed, bid} to {false, null}
try
tell application id “com.apple.systemevents”
set bid to bundle identifier of application process anAppName
end tell
on error
set failed to true
end try
if failed and (length of anAppName = 4) then
try
local an
set an to name of application id anAppName
tell application id “com.apple.systemevents”
set bid to bundle identifier of application process an
end tell
end try
end if
if bid ≠null then
return bid
else
return “”
end if
end run
END!
)
if [ ! $the_res ] ; then
echo “No app with that denotation found.”
phelp
exit 1
else
echo $the_res
fi[/code]

crea returns the creator code, if given the correct appname, or bundle identifier. The sheer purpose is to be able to get the creator code so I for instance can type open -b bid MACS to get the frontmost finder window in front of the terminal window I currently use.

[code]#! /bin/bash

© McUsr 2012 and put in Public domain.

You may not post this elsewhere or put it into a repository

declare prgname version Usage ;
prgname=basename $0
PATH=“/bin:/usr/bin”
version=“0”
Usage=“Usage : $prgname appname”

phelp()
{
echo "
$Usage
Returns the creator code if it can finds something looking like a: an appname, or b: the bundle identifier.
Case is sensitive." 1>&2

}
if [ $# -ne 1 ] ; then
echo “Bad numbers of parameters” 1>&2
phelp
exit 1
elif [ “$1” = “-h” ] ; then
phelp
exit 0
fi
the_res=$(osascript <<END! - “$1”
on run {anAppName}
local failed, crea
set {failed, crea} to {false, null}
try
tell application id “com.apple.systemevents”
set crea to creator type of application process anAppName
end tell
on error
set failed to true
end try
if failed and (length of anAppName > 4) then
try
local an
set an to name of application id anAppName
tell application id “com.apple.systemevents”
set crea to creator type of application process an
end tell
end try
end if
if crea ≠null then
return crea
else
return “”
end if
end run
END!
)
if [ ! $the_res ] ; then
echo “No app with that denotation found.”
phelp
exit 1
else
echo $the_res
fi[/code]

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]

Hello!

Using FreeMind, is great, not only the price, but I actually at least had a license for an app for mindmapps, that came with a rather unsober price-tag in the range (200 euro or something). I’m happy with FreeMind, except for one thing, it uses the .mm extension, which means when I double click the file, the xml opens up in XCode.

I was anxious to know whether I would manage to set the default app, or more precisely that I would manage to get to its data, FreeMind being written in Java.

It turned out that it worked pretty well, so now it is possible for you to, should you wish to make dot files for GraphWiz or whatever open in that app.

Just remember, after doing this, deleting .DS_Store files in those files home directories becomes a very bad idea.



tell application "System Events"
	
	tell application process "FreeMind"
		set theDocTitle to name of its front window
		set bid to its bundle identifier
	end tell
	# this way will work with most apps that doesn't set their 
	# bundle identifier.
	# if it isn't a java app, or something, you can always try to make the 
	# app scriptable, or a copy of the original app if you are on Mountain Lion
	# log theDocTitle
	# return 
	set pathStart to offset of "/" in theDocTitle
	set thePath to text pathStart thru -1 of theDocTitle
	# this step initially to see how things are that is what we'we got in the windowname.
	# I have seen the posix path is  here, even if it isn't shown.
	
	set appPath to path to application "FreeMind" as alias
	set default application of disk item thePath to appPath
end tell

This is too small to get its own post.

The thing is, that the best way to make quicklook show text files correctly, is to set the settings in TextEdit, but this doesn’t work for merge tools like FileMerge for instance, when there are no BOM in the file, hence this little handler, that you may incorporate as you wish.

You should apply it as soon as you create the files. Really! :slight_smile:

to forceUTF8 for pxFile
	do shell script "xattr -w com.apple.TextEncoding \"UTF-8;134217984\"  " & quoted form of pxFile
end forceUTF8

Hello.

This is a command line utility, that sets/selects default application for a file.

Glad I found it before I made it. (On my harddrive. :slight_smile:

A type identifier is just a special case of all the UTIs that a particular file conforms to. This is a list, not 1 result, so I advise the author of post to think about how to get this list of all UTI for the file.


set aPOSIXPath to quoted form of (POSIX path of (choose file))
set theUTIs to do shell script "mdls -name kMDItemContentTypeTree " & aPOSIXPath

set theUTIs to paragraphs 2 thru -2 of theUTIs
repeat with i from 1 to (count of theUTIs)
	set item i of theUTIs to word 1 of (item i of theUTIs)
end repeat

return theUTIs

I saved this script in my Scripts folder with name “Get UTIs list of the file”. To execute a script from the command line, convert it into an osascript form. I do not use osascript, so I could be wrong.

NOTE: You can use choose folder as well. The folders have UTIs like files, but this is unlikely to ever come in handy.