Specific Finder scripting

I want a specific finder script (well, droplet actually) and everything I try won’t work, so i’m just going to post a list of what I want.

this is a droplet, if you didn’t catch it above.

  1. If the file is a .rtf or .rtfd: move it to Macintosh HD/Users/my username/Library/Scripts/iTunes/Documentation AND
    If the file is named Read Me: Ask me what to rename it to (and obviously rename it to that)
  2. If the file is a .app: Ask me where to move it to, with the default location being Macintosh HD/Users/my username/Library/Scripts/iTunes
  3. If the file is a .scpt: open it in Script Editor
  4. If the file is a .webloc: Ignore it
  5. If the file is anything else: Ask me where I want to move it to and whether I want to rename it.

For all of these, it would be preferable, although not necessary, to have the name in the dialog box.
I also would like it to do all this with only one drag-and-drop of multiple file types.

Oh, all right, here is my script so far, if you insist.

on open theItems
	repeat
		set thefile to item of theItems
		set fileName to name extension of thefile
		set theDestinationFolder to (choose folder with prompt "Choose where you want your documentation to go")
		if fileName is "rtf" then
			move thefile to theDestinationFolder
		else if item name ends with ".rtfd" then
			move thefile to theDestinationFolder
		end if
	end repeat
end open

Note that some of that was workarounds to see if a different variation worked- what I want is not necessarily what is in the script, it is in the “What I Want” section above it.

Thanks ENORMOUSLY!!

P.S. What is applescript Studio? I saw it around and wondered what it was.

Save this file as an application.
Make sure you change the paths to the correct paths for your machine.

Regards,

Craig


--IMPORTANT: Change these to actual path
property rtfFile : "Macintosh HD:Users:my  username:Library:Scripts:iTunes:Documentation:"
property appFile : "Macintosh HD:Users:my  username:Library:Scripts:iTunes:"

on open theseItems
	--repeat through all the items dropped
	repeat with i from 1 to count of theseItems
		set thisItem to item i of theseItems as alias
		set nameExtension to name extension of (get info for thisItem)
		set theName to name of (get info for thisItem)
		
		if theName = "Read Me" then
			set newName to text returned of (display dialog "What would you like to name this file?" default answer theName)
			my renameFile(thisItem, newName)
			my moveThisFile(thisItem, rtfFile)
			
		else if nameExtension = "rtf" or nameExtension = "rtfd" then
			my moveThisFile(thisItem, rtfFile)
			
		else if nameExtension = "app" then
			set newLocation to choose folder with prompt "Where would you like to move this file?" default location appFile
			my moveThisFile(thisItem, newLocation)
			
		else if nameExtension = "scpt" then
			tell application "Script Editor" to open thisItem
			
		else if nameExtension = "webloc" then
			--do nothing			
		else
			set newName to text returned of (display dialog "What would you like to name this file?" default answer theName)
			set newLocation to choose folder with prompt "Where would you like to move this file?"
			my renameFile(thisItem, newName)
			my moveThisFile(thisItem, newLocation)
			
		end if
		
	end repeat
	
end open

on moveThisFile(theFile, thelocation)
	tell application "Finder"
		move theFile to thelocation
	end tell
end moveThisFile

on renameFile(theFile, newName)
	tell application "Finder"
		set name of theFile to newName
	end tell
end renameFile

Craig, you are amazing. I do have a few questions about applescript in general, but you don’t have to answer them if you’re busy (This goes for everyone else, too).

Before you look at these, I modified the script so the default location for any non-rtf/rtfd/app/scpt/webloc file is my Scripts folder- and I inserted my username, because it can’t hurt. I also added “txt” to the rtf or rtfd action.

Here is the new version.

--IMPORTANT: Change these to actual path
property rtfFile : "Macintosh HD:Users:willie:Library:Scripts:iTunes:Documentation:"
property appFile : "Macintosh HD:Users:willie:Library:Scripts:iTunes:"
property unknownFile : "Macintosh HD:Users:willie:Library:Scripts:"

on open theseItems
	--repeat through all the items dropped
	repeat with i from 1 to count of theseItems
		set thisItem to item i of theseItems as alias
		set nameExtension to name extension of (get info for thisItem)
		set theName to name of (get info for thisItem)
		if theName = "Read Me" then
			set newName to text returned of (display dialog "What would you like to name this file?" default answer theName)
			my renameFile(thisItem, newName)
			my moveThisFile(thisItem, rtfFile)
			
		else if nameExtension = "rtf" or nameExtension = "rtfd" or nameExtension = "txt" then
			my moveThisFile(thisItem, rtfFile)
			
		else if nameExtension = "app" then
			set newLocation to choose folder with prompt "Where would you like to move this file?" default location appFile
			my moveThisFile(thisItem, newLocation)
			
		else if nameExtension = "scpt" then
			tell application "Script Editor" to open thisItem
			
		else if nameExtension = "webloc" then
			--do nothing			
		else
			set newName to text returned of (display dialog "What would you like to name this file?" default answer theName)
			set newLocation to choose folder with prompt "Where would you like to move this file?" default location unknownFile
			my renameFile(thisItem, newName)
			my moveThisFile(thisItem, newLocation)
			
		end if
		
	end repeat
	
end open

on moveThisFile(thefile, thelocation)
	tell application "Finder"
		move thefile to thelocation
	end tell
end moveThisFile

on renameFile(thefile, newName)
	tell application "Finder"
		set name of thefile to newName
	end tell
end renameFile

And now…

  1. What is the difference between a property and a variable?
  2. Does “is” mean the same thing as"=" (equals sign)?
  3. What does “my” do?
  4. How can you reference variables before you define them?
  5. Was my only problem with the paths the last colon (which I didn’t add)?
  6. How can I modify the script so that
    a. I drag a disk image/folder to it, it does it’s thing with the files on/in the disk image/folder, even if they’re in their own a folder, and then ejects the disk image OR
    b. It ejects the disk image that I dragged the files from after it does it’s thing.
    Preferably A.
  7. This might seem a little ambitious, but can I modify the “scpt” part to tell Script Editor to save the script as an Application Bundle (NOT run-only, stay open, or with a startup screen) in the way app files were saved, and then to quit and move the scpt to “Macintosh HD:Users:willie:Library:iTunes:Scripts:Application-ed:”?
  8. This is pretty specific too, but I want to tell Bookpedia (a non-scriptable app, as far as I know) to set either Custom 1 or Sort Author (Because my first custom field is named Sort Author) to Last, First of the data in Author, formatted in First Last format. I don’t expect you to know this, but I thought I’d ask.
  9. Is there a way that I can get my scripts to not show up in the Dock or Application Switcher? I tried Dock Dodger, which told me “*.app is not an application” and Dockless, which just plain didn’t work.
  10. What is AppleScript Studio?
  11. HOW DID YOU WRITE THAT SO FAST?!? Will I be like that one day?
  12. Can you take a look at my other post at http://bbs.macscripter.net/viewtopic.php?id=24339? Please?

Again, you don’t need to answer me (you’ve probably wasted enough time just reading this post) but I’d greatly appreciate it if you do, even though half of your responses will be “There is no way to make this happen.”
Thanks!

Here you go. :wink:

Thanks- you’re a great help. I have a few problems/questions.

Can I modify this so it will save in the same folder as it would if I had dragged it on as an application, with the same name as the original file, and as an application bundle rather than an application? Here is my attempt, correct me if any of it is wrong. Additionally, I told it to quit Script Editor because otherwise it would save the first document I opened as test- will this allow me to save changes? And is there a way to reopen with the same windows afterward?

(*else if nameExtension = "scpt" 
-- I have to comment this out, because i'm working in a seperate file
*)
set appName to name of thisItem
set scriptPath to "Macintosh HD:Users:willie:Library:Scripts:iTunes:" & appName
tell application "Script Editor"
	quit --will this ask me to save changes?
	open thisItem
	save document 1 as "application bundle" in scriptPath
	close document 1
	launch --how can I make this launch with the windows I had before I closed it?
end tell

Also, about GUI Scripting- can you set a variable to something akin to the artist tag in iTunes, and then copy it to something akin to sort artist? There is an info box it that will help.
And, I want to set a variable like firstName to the text after the last space in the tag akin to an artist, unless that text is “Jr.” or something of that nature- I could figure out those articles myself. Is there a way to to this?
Let me explain further: I want to turn something that may be as complicated as “Bob Smith, Jr. and Jane Mary Johnson III” to “Smith, Bob Jr. and Johnson, Jane Mary III” although turning “Bob Smith” to "Smith, Bob"would be an enormous help too.

About the other project, I think I know what you mean- essentially I’m making something like “authorDialog” stand for something like
“set authorName to (display dialog “Author Name (or Names)” & return & return & “For more than one name, preface the last one with "and"” default answer “Last, First” buttons {“Next”} default button 1)
if button returned of authorName is “Next” then
set author to text returned in authorName
end if”
Is that correct?

You are really helpful, I’ll be sure to check out those books, and I hope you enjoyed your movie.

I just tested the script, with the contents of the folder Track Names to Filenames v2.6 Æ’, which I got at http://dougscripts.com/itunes/scripts/ss.php?sp=itnom2fnom.
First it asked me “What would you like to name is file?” with the default answer “Track Names → Filenames”- all good so far. But then…
A dialog box came up saying “Can’t make “Macintosh HD:Users:willie:Library:Scripts:” into type alias.” with an OK and Edit button.
What did I do wrong? Here is the version I was using:


--IMPORTANT: Change these to actual path
property rtfFile : "Macintosh HD:Users:willie:Library:Scripts:iTunes:Documentation:"
property appFile : "Macintosh HD:Users:willie:Library:Scripts:iTunes:"
property unknownFile : "Macintosh HD:Users:willie:Library:Scripts:"

on open theseItems
	--repeat through all the items dropped
	repeat with i from 1 to count of theseItems
		set thisItem to item i of theseItems as alias
		set nameExtension to name extension of (get info for thisItem)
		set theName to name of (get info for thisItem)
		if theName = "Read Me" then
			set newName to text returned of (display dialog "What would you like to name this file?" default answer theName)
			my renameFile(thisItem, newName)
			my moveThisFile(thisItem, rtfFile)
			
		else if nameExtension = "rtf" or nameExtension = "rtfd" or nameExtension = "txt" then
			my moveThisFile(thisItem, rtfFile)
			
		else if nameExtension = "app" then
			set newLocation to choose folder with prompt "Where would you like to move this file?" default location appFile
			my moveThisFile(thisItem, newLocation)
			
		else if nameExtension = "scpt" then
			tell application "Script Editor"
				if running is true then display dialog "Please quit script editor before proceeding." buttons "OK" default button 1
			end tell
			tell application "Script Editor" to open thisItem
			set scriptPath to (path to desktop as Unicode text) & "test"
			tell application "Script Editor"
				save document 1 as "application" in scriptPath
				close document 1
			end tell
		else if nameExtension = "webloc" then
			--do nothing			
		else
			set newName to text returned of (display dialog "What would you like to name this file?" default answer theName)
			set newLocation to choose folder with prompt "Where would you like to move this file?" default location unknownFile
			my renameFile(thisItem, newName)
			my moveThisFile(thisItem, newLocation)
			
		end if
		
	end repeat
	
end open

on moveThisFile(thefile, thelocation)
	tell application "Finder"
		move thefile to thelocation
	end tell
end moveThisFile

on renameFile(thefile, newName)
	tell application "Finder"
		set name of thefile to newName
	end tell
end renameFile