Get File Name (how to)

I need a drag and drop applescript that simply returns the name of the file dropped on the applet

on open
--return the file name of me, so I can use me as a variable
end open

on open {anAlias} -- alias of first file/folder/etc. dropped
	tell application "Finder" to set itmName to name of item anAlias -- (note: name is unicode text)
	set the clipboard to itmName -- do whatever
	return
end open

Thanks HAS, that works perfectly.

Since it returns Unicode text, it might be working, but not as expected. Try this one, saved as an application (tested on 10.2.3):

on open {anAlias} -- alias of first file/folder/etc. dropped 
	tell application "Finder" to set itmName to name of item anAlias -- (note: name is unicode text) 
	set the clipboard to itmName as text -- do whatever 
	return
end open

Ah, yes, that worked. It now appears in the clipboard properly. It was in string format before wasn’t it? Which would explain why it wasn’t showing up in the clipboard… :oops: Thanks for you help. :slight_smile:

Er, forget that last post.

OK, taking comments from another post you made I have attempted to modify this script. First, here is the working code that I want to turn into a drag and drop app:


set russafrussa to (choose file)
set the clipboard to russafrussa as text

That code, obviously, launches a file requester and then pastes the file path and name into the clipboard. Very useful for getting files (but setting the variable to alias as opposed to the OS9 way of file was what was tripping me up before…) Adding “on open” and “end open” transforms the app above into a drag&drop applet, but it doesn’t parse the file info I want.

So I made a simple change and tada!


on open {anAlias} -- alias of first file/folder/etc. dropped
	set russafrussa to anAlias
	set the clipboard to russafrussa as text
end open

Feeling cocky with my applescripting power, I tried to combine both versions of my script into one. Drag a file onto it, get the file path. Doubleclick on it, and get the file requester. Here I bumped into the wall of my ignorance. LOL


on open {anAlias} -- alias of first file/folder/etc. dropped
	if anAlias is "" then
		set russafrussa to (choose file)
		otherwise
		set russafrussa to anAlias
	end if
	set the clipboard to russafrussa as text
end open

Doubleclicking on the app doesn’t produce any results I can see. No requester and no new clipboard information. Why isn’t this working the way I think it should be. :slight_smile:

Lastly, looking over your code, simply changing name to path didn’t produce the results I wanted. I received a Class FTPc error, whatever that is… More OSX goodness I’m sure.

on open {anAlias} -- alias of first file/folder/etc. dropped
	tell application "Finder" to set itmName to path of item anAlias -- (note: name is unicode text)
	set the clipboard to itmName as text -- Tada! I can paste it now.
	return  -- Why is this here?
end open

Why didn’t “path” work and what should I have stated instead?

Douglas
[/b]

I succeeded in getting the script to work. I wonder though if there is a more elegant solution. What do you think? And can somebody kindly answer some of the questions I’ve posted even though I’ve solved the problem? Thanks.

set anAlias to ""
on open {anAlias} -- alias of first file/folder/etc. dropped
	set russafrussa to anAlias
	set the clipboard to russafrussa as text
end open
if anAlias is "" then
	set russafrussa to (choose file)
end if

Douglas

I’m not exactly sure what you want to do. Do you want the script to place the contents of a file onto the clipboard? If so, you need to look at the ‘read’ command in Standard Additions.

Regarding droplet behavior …

on open {anAlias}
-- do stuff to anAlias
end open

In the snippet above, anAlias contains a reference to the file or folder dropped onto the droplet. The ‘on open’ doesn’t actually open anything. If you want the script to do something when double-clicked, you need the ‘on run’ handler. You can use both in the same script, as is often done to advise users how to use a script.

on open {anAlias}
	-- do some stuff to/with anAlias
end open

on run -- This happens when someone double-clicks the script's icon.
	display dialog "To use this droplet, drop a file or folder onto its icon."
end run

Now that I’ve added more confusion to the topic, what’s the next question? :wink:

Hi Rob, HAS, TLI and Everyone else… I needed the dropped files name so that I could use it as a variable to make the following code work…

on open {anAlias}
	tell application "Finder" to set discName to name of item anAlias --HAS tip to get the dropped file name as a variable
	do shell script "hdiutil unmount "/Volumes/" & discName & """
	get word 1 of result
	set devName to result
	do shell script "hdiutil convert /dev/" & devName & " -format UDTO -o "$HOME/Desktop/" & discName & """
	display dialog "Enter the Amount of Time (in seconds) it Takes to Copy a Disc on Your Machine" default answer ""
	set copyTime to (text returned of result) + 120 -- add two minutes, for prosperities sake!!
	do shell script "sleep " & copyTime
	display dialog "Before Hitting "OK". Eject the Original Disc Named ”" & discName & "” By Hitting the Eject Button on Your Keyboard" buttons {"OK"} default button "OK" giving up after 10
	tell application "Finder"
		activate
		display dialog "To Burn a Copy of ”" & discName & "” " & return & "Hit the "OK" Button" default button "OK" giving up after 10
	end tell
	do shell script "cd $HOME/Desktop;hdiutil burn "" & discName & ".dmg" -noverifyburn -noeject"
end open

Many thanks to HAS, Rob, TLI, & everyone else for the hints that had eluded me for hours.
Anyhow, the above Applescript still needs a bit of UI help. The first try allowed the terminal.app to show the progress, but that was kinda ugly.(Chime in ifin you wish, ideas welcome)

Basically, the above code is designed to make a copy of a CD-Rom that is dropped on the App’s icon. It lacks a few interface elements, but it does work…

That was it! Thanks… “on run” was the last touch my little script needed. I also needed to copy the result to the clipboard. Now it works like a champ. Thanks again.

D Cootey