The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   UNIX - General (http://hintsforums.macworld.com/forumdisplay.php?f=16)
-   -   How to create a Finder alias from shell script? (http://hintsforums.macworld.com/showthread.php?t=18644)

StickyC 12-16-2003 08:58 PM

How to create a Finder alias from shell script?
 
How do I create a Finder alias (which is not the same as a Symbolic or Hard Link) to a file from a shell script? (IE - the same thing as Ctrl-clicking a file and selecting "Make Alias")

I know I can set a file's attribute using SetFile -aA to tell Finder it's an alias file, but how do I define what it's an alias to?

mervTormel 12-16-2003 10:29 PM

hmm, well, i don't think there is a command to do this; craft a finder-style alias from the shell.

could possibly be done with osascript ?

robJ 12-16-2003 10:43 PM

This appears to work:

Code:

osascript -e 'tell application "Finder" to make alias file to alias "path:to:original file" at desktop'
-- Rob

StickyC 12-17-2003 01:31 PM

Quote:

Originally posted by robJ
This appears to work:

Code:

osascript -e 'tell application "Finder" to make alias file to alias "path:to:original file" at desktop'
-- Rob
I'm trying to make aliases to folders within packages in a set of subfolders (in an array), so I'm trying something like:

osascript -e 'tell application "Finder" to make alias file to alias "source folder:${MyDirectory[x]}:folder" at "target folder:${MyDirectory[x]}:My Package.app:Contents:Resources"'

It just doesn't seem to work, no matter how I write the paths (with / instead of :, escaping the quotes, using "folder x of folder y of folder z", etc.)
I can create the target on the desktop and MvMac it, so things are working.

robJ 12-17-2003 01:41 PM

I reckon you'll need to convert your variables to something that Finder understands. I doubt that it knows what '${MyDirectory[x]}' means (I don't know either! :p).

-- Rob (the non-Unix one)

mervTormel 12-17-2003 02:06 PM

single-quoted strings ( 'foo' ) turn off variable interpolation in the shell, so the variable reference in 'blah ${foo} blah' is just that string of characters.

you need to craft the scriptage in double-quotes so that the variable references ( ${foo} ) are resolved.

use a 'here doc' ; here's a sample ...

Code:

osascript <<_eof
tell app "finder"
activate
display dialog "$TERM"
end tell
_eof

/edit: simplify example

StickyC 12-17-2003 05:47 PM

That does seem to solve the source problems. I still can't seem to create the alias inside a package, it just throws "Can't get alias file" errors.

vonleigh 12-17-2003 06:07 PM

Why does it have to be a finder alias versus a symlink?

v

StickyC 12-17-2003 06:25 PM

The short of it is, we're supporting someone elses code that requires it to be a finder link.

Mucho frustrating.

robJ 12-17-2003 09:54 PM

I just tested a simple AppleScript script and it is possible to create an alias within a package. What type of paths are you passing to the Finder. It understands only colon delimited paths.
Code:

path:to:file
You can convert slash delimited paths with something like this:
Code:

set foo to "/usr/bin/"
set bar to POSIX file foo as text

-- Rob

hayne 12-17-2003 10:02 PM

script
 
Here's a Bash script for making Finder-style aliases:
Code:

#!/bin/sh

# make_alias
# This script takes two command-line arguments:
# 1) The name (relative or full path) of a source file or folder (directory)
# 2) The name (relative or full path) of a destination folder (directory)
# The script makes a Finder-style alias to the source file or folder
# and puts it in the specified destination folder.
#
# Cameron Hayne (macdev@hayne.net), December 2003
# Modified as suggested by Paul Russell (prussell@sonic.net), May 2006
#          to allow either file or folder as source
# Mofified to work with ".app" files, January 2007

scriptname=`basename $0`
if [ $# -lt 2 ]; then
    echo "Usage: $scriptname srcPath destPath"
    exit
fi

srcPath=$1
destPath=$2

if [ ! -e $srcPath ]; then
    echo "$scriptname: $srcPath: No such file or directory"
    exit
fi

# remove possible trailing slash from $srcPath
srcPath=${srcPath%/}

# set $srcType to "file" or "folder" as appropriate
if [ -d $srcPath ]; then
    if [ "${srcPath##*.}" == "app" ]; then
        srcType="file"
    else
        srcType="folder"
    fi
else
    srcType="file"
fi

if [ ! -d $destPath ]; then
    echo "$scriptname: $destPath: No such directory"
    exit
fi

case $srcPath in
/*) fullSrcPath=$srcPath ;;
~*) fullSrcPath=$srcPath ;;
*)  fullSrcPath=`pwd`/$srcPath ;;
esac

case $destPath in
/*) fullDestPath=$destPath ;;
~*) fullDestPath=$destPath ;;
*)  fullDestPath=`pwd`/$destPath ;;
esac

/usr/bin/osascript > /dev/null <<EOT
tell application "Finder"
    set macSrcPath to POSIX file "$fullSrcPath" as text
    set macDestPath to POSIX file "$fullDestPath" as text
    make new alias file to $srcType macSrcPath at folder macDestPath
end tell
EOT

Example of use:
make_alias myfile ~/Desktop

StickyC 12-18-2003 12:38 PM

Aha! It's not the target that's killing it, it's the source. I'm trying to make an alias of a folder and that's what's not working. Changing the file to folder in the script fixed that.

Thanks for all the help gang!

hayne 05-31-2006 09:53 PM

Quote:

Originally Posted by StickyC
I'm trying to make an alias of a folder and that's what's not working. Changing the file to folder in the script fixed that

As suggested by Paul Russell, I have modified the above script to automatically determine whether you are making an alias to a file or to a folder and handle either case.

T618 07-05-2006 04:33 PM

thanks
 
I just wanted to say thanks for the shell script. It solved a problem I had too, where creating an alias from the finder was not possible.

hayne 01-30-2007 12:02 AM

I have modified the above 'make_alias' script so that it works with ".app" files. (These are folders according to Unix, but we need to treat them as files when making Finder aliases to them.)

zredbaron 04-10-2008 10:46 AM

great script, but how can it be modified to set a .icns file to be displayed on the alias?

thanks!

robinwmills 04-15-2008 01:54 PM

I agree. Haynie's a genius. Great script.

I copied the resource fork to a short cut and that worked.
Code:

mkalias ~/Pictures/..../A.jpg ~/Desktop
sips --addIcon ~/Pictures/..../A.jpg
cp ~/Pictures/.../A.jpg/..namedfork/rsrc ~/Desktop/A.jpg/..namedfork/rsrc

Some other points:
1) This didn't work when I copied compass.icns from Safari.app.

2) (shock horror). I had to modify haynie's $srcPath variables to "${srcPath}" to get the script to work on files with spaces in their names.

3) Beware of sips --addIcon on Leopard (spoils Coverflow, because Finder uses the low res icon). I've got a fix if anybody's interested (this is way off subject).

TommyWillB 07-20-2008 03:01 PM

Quote:

Originally Posted by hayne (Post 297658)
As suggested by Paul Russell, I have modified the above script to automatically determine whether you are making an alias to a file or to a folder and handle either case.

Hayne, could you post your modified code?

I'm trying t make a list of alises to image folders so that iTunes can synch them to my AppleTV... and it doesn't seem to follow symbolic links. :-/

-----

Never mind... I mis-read. I see your script DOES accept folder.

It'd be cool if I could pass the filename of the alias instead of it's enclosing folder.

TommyWillB 07-20-2008 03:19 PM

Shoot!

Turns out iTunes' AppleTV photo synch won't follow aliases EITHER!

So now I'm not sure how to grab (selected) photos that are spread all around my hard drive.

:-/

cwtnospam 07-20-2008 05:22 PM

Finder Smart Folder, then drag & drop selection(s) from the results?

TommyWillB 07-28-2008 12:38 PM

Wow! That's a great idea!

I'll try that when I'm back on my Mac. (Must use WinDoze for Work :-( )

TommyWillB 07-31-2008 06:48 PM

I'm so gonna pull my hair out!

When you browse to a Smart Folder, it's grey'd out, so you can't select it.

So where exactly would I "drag and drop these"?

cwtnospam 07-31-2008 06:59 PM

Smart Folders allow me to select items, both in the Finder and in iTunes. If you create a Smart Folder in the Finder, you should be able to drag & drop movies into iTunes, unless they're a format that isn't supported.

Maybe if you describe exactly what steps you're taking and where the problem occurs we can figure out what's going wrong.

tw 08-01-2008 01:38 AM

I'll point out that one of the canned sidebar searches that the Finder offers is "all images". Go to Finder preferences, Sidebar tab, third from the bottom. this will gather all the images on your hard drive, and you can move them where and as you like.


All times are GMT -5. The time now is 06:11 PM.

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.