+1  A: 

As an example, you can work with the Finder instead of a shell script to get the name of a single file that is dropped on the script that is saved as an application. If you don't need the display dialog, you can remove it, but you have the file name as a variable to work with:

on open the_files
    repeat with i from 1 to the count of the_files
        tell application "Finder"
            set myFileName to name of (item i of the_files)
        end tell
        display dialog "The file's name is " & myFileName
    end repeat
end open
songdogtech
+1  A: 

I'm not sure what precisely you're trying to do, but I have a guess. Is the idea that you want to take every file dropped on the script and create a symbolic link to each one on the Desktop? So if I drop ~/look/at/me and ~/an/example, you'll have ~/Desktop/me and ~/Desktop/example? If that's what you want, then you're in luck: ln -s <file1> <file2> ... <directory> does exactly that. (Edit: Although you have to watch out for the two-argument case.) Thus, your code could look like this:

-- EDITED: Added the conditional setting of `dest` to prevent errors in the
-- two-arguments-to-ln case (see my comment).

on quoted(f)
    return quoted form of POSIX path of f
end quoted

on open filelist
    if filelist is {} then return
    set dest to missing value
    if (count of filelist) is 1 then
        tell application "System Events" to set n to the name of item 1 of filelist
        set dest to (path to desktop as string) & n
    else
        set dest to path to desktop
    end if
    set cmd to "ln -s"
    repeat with f in filelist & dest
        set cmd to cmd & " " & quoted(f)
    end repeat
    do shell script cmd
end open

Note the use of quoted form of; it wraps its argument in single quotes so executing in in the shell won't do anything funny.

If you want to get at the name of the file for another reason, you don't need to call out to the Finder; you can use System Events instead:

tell application "System Events" to get name of myAlias

will return the name of the file stored in myAlias.


Edit: If you want to do something to a single file, it's pretty easy. Instead of using repeat to iterate over every file, just perform the same action on the first file, accessed by item 1 of theList. So in this case, you might want something like this:

-- EDITED: Fixed the "linking a directory" case (see my comment).

on quoted(f)
    return quoted form of POSIX path of f
end quoted

on open filelist
    if filelist is {} then return
    set f to item 1 of filelist
    tell application "System Events" to set n to the name of f
    do shell script "ln -s " & ¬
        quoted(f) & " " & quoted((path to desktop as string) & n)
end open

It's pretty much the same, but we grab the first item in filelist and ignore the rest. Additionally, at the end, we display a dialog containing the name of the symlink, so the user knows what just happened.

Antal S-Z
Thnks, what I actually want to do is for a single file. I used the repeat because I got that example from a website and don't know how to modify it for a single file.
Petruza
Edit: it works great for files, but it fails for folders, which is what I'll want to lynk the most. The error message is: "In: /Users/petruza/Desktop/: File exists" there's no file on the desktop with the same name of the original file, and this works with regular files, so the path is right. It seems that when getting a folder dropped on the script, it interprets it as '/' which is what's added to the desktop path on the error message. If I add a trailing slash to the path, the error shows two trailing slashes.
Petruza
I think it'll be better if I myself learn applescript. can you recommend a good tutorial?
Petruza
Oh crap, it not working is my fault—if you try to link a single folder to a folder, it will try to replace it, and you don't want that. Let me fix it!
Antal S-Z
Alright, fixed. As for your other question: *good for you* for taking initiative and learning the language! I'm not actually sure myself (I learned by experimenting, which is a good plan), but I would post a separate question about it—there should be general interest.
Antal S-Z