Append username to name of file

For example, if my username is jir107, I’m logged in as jir107, when I drop a file named “application.doc” into a folder it will get renamed to “jir107_application.doc” Does anybody know how I can do this in Applescript? Thanks!

Model: MacBook
Browser: Firefox 2.0
Operating System: Mac OS X (10.4)

Hi,

welcome to MacScripter.

Here is a simple droplet. Save it as an application and drop the files onto it.
the script takes always the short user name of the current user.
Renaming a file using a folder action causes an infinitive loop in the hot folder itself.


on run
	display dialog "Drop files or folders onto the icon to rename them" buttons {"OK"} default button 1
end run

on open theseFiles
	set userName to short user name of (system info)
	repeat with oneFile in theseFiles
		tell application "Finder" to set name of oneFile to (userName & "_" & (get name of oneFile))
	end repeat
end open

this is similar to one of the problems that I am trying to solve, maybe you can help me too. I want to rename the files based upon a file name in an excel (or text file) list. I can organize the list to have the desired filename in one column and the path/location of the current file which I would like renamed in another column.

Any ideas?

Assuming the new file names in the first column and the HFS(!) paths in the second, try this


set excelFile to choose file with prompt "Choose the Excel file" of type {"XLS6", "XLS7", "XLS8"}
tell application "Microsoft Excel"
	open excelFile
	set P to value of used range of active sheet
end tell
repeat with i in P
	tell application "Finder" to set name of file (item 2 of i) to item 1 of i
end repeat

no soup.

excel opens file, and then error:

Finder got an error: Can’t set name of file “Macintosh HD:Users:…”

I think it is the path that I am using and I’m not good with terminal, maybe you can give me the proper terminal path?

I tried:
“Macintosh HD:Users…”
“Macintosh HD/Users…”
“Macintosh HD\Users…”

The colon separated paths are correct
I’ve tested the script on my machine and it works fine.

10.5.2, Excel 2004

you can try a explicit dereferencing


.
repeat with i in P
	set {theName, theFile} to contents of items of i
	tell application "Finder" to set name of file theFile to theName
end repeat
.

StefanK, Thank You. I left out the : bw the folder and filename.

Any chance you can solve this one too. It requires knowledge of Entourage. I have a list of outgoing mail that has already been created using email merge. I would like to attach the files that I just renamed thereto.

try this:



set excelFile to choose file with prompt "Choose the Excel file" of type {"XLS6", "XLS7", "XLS8"}
tell application "Microsoft Excel"
	open excelFile
	set P to value of used range of active sheet
end tell
repeat with i in P
	set theFile to item 2 of i as alias
	tell application "Finder" to set name of theFile to item 1 of i
	
	tell application "Microsoft Entourage"
		activate
		try
			set myMessage to make new outgoing message with properties ¬
				{to recipients:"john@doe.com", subject:"Send File ", attachment:theFile}
			open myMessage
		end try
	end tell
end repeat

You my friend are impressive.

It worked and you provided me with everything that most people would need to tweak it, but I am limited. This created a new message. I think there may be two solutions. Add the text that I already added to the email to this script or (preferred) figure out a way to have the script locate the outgoing message. I put the source code (from entourage) from the message that was created using the script you provided and the source code from one of the messages in the outbox. I don’t know if this will help.

outbox message

User-Agent: Microsoft-Entourage/11.3.6.070618
Date: Tue, 12 Feb 2008 21:52:53 -0500
Subject: Securities
From: justin justin@hithere.net
To: john@anywhere.com
Message-ID: C3D9DFA3.35A%john@anywhere.net
Thread-Topic: Securities
Mime-version: 1.0
Content-type: text/plain;
charset=“US-ASCII”
Content-transfer-encoding: 7bit

[Below is the text already in the message or that I would want to add and is part of the source code above]

Mr. whoever:

For the sake of time I will get right to it. This is the end of the message.

Script Created Message

User-Agent: Microsoft-Entourage/11.3.6.070618
Date: Thu, 14 Feb 2008 13:49:21 -0500
Subject: Send File
From: justin justin@hithere.net
To: john@anywhere.net
Message-ID: C3D9F8EA.35C%john@anywhere.net
Thread-Topic: Send File
Mime-version: 1.0
Content-type: multipart/mixed;
boundary=“B_3285842163_618116”

This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

–B_3285842163_618116
Content-type: text/plain;
charset=“US-ASCII”
Content-transfer-encoding: 7bit

–B_3285842163_618116
Content-type: application/octet-stream; name=“Filename.pdf”
Content-disposition: attachment;
filename=“Filename.pdf”
Content-transfer-encoding: base64

It also appears that the Message-ID: are in sequential order and would line up with the excel list. the only thing that changes are the last two numbers “39” to 40" etc. Message-ID: <C3D9DFA3.35A%john@anywhere.net>