How can I get the file name?

First off, I am very new at this and have never done anything like it before. Also, I’ve been looking through the forums hoping to find an answer to my question. I haven’t found an exact match yet, though I will continue looking through the 400+ pages.

I’ll try and explain everything I’m trying to do right off the bat. I would like to drag a file onto the script (using on open, right?), then get the file name and probably store it as a variable, then append the file with a pre set string after the original file name and before the extension. I know I can use keystroke to input the string I want, but how do I put the string between the original file name and it’s extension?

for example (exactly what I’m doing): i drag my input file (named “AA_12.pdf”) onto the script, then it appends 1-12, the final output being named “AA_12-1-12.pdf” I will then do it again instead appending 2-11 so on and so on.

I hope I have made everything clear, let me know if I haven’t. Thanks for anyone’s help, I look forward to becoming a part of this community.

e: does this forum have an irc channel? or could someone point me to one elsewhere?

I’m not quite certain I understand what you want to add to the end of the filename. I know it’s “1-12” but then what?

Each time a new file is dropped do you want to increase/decrease {2-11,3-10,4-9,.} or do you only want to change the preset for additional files being processed on the initial drop? (Hopefully that makes sense)

Anyways here’s a example to get you started that will add the “1-12” to every file that’s dropped on it.

on open fileList
	set preSet to "1-12"
	tell application "Finder"
		repeat with i from 1 to count of items of fileList
			set nameExt to name extension of (item i of fileList)
			set oldName to text 1 through -((count nameExt) + 2) of (get name of (item i of fileList))
			set name of (item i of fileList) to oldName & "-" & preSet & "." & nameExt
		end repeat
	end tell
end open

To begin with, I have one pdf with multiple spreads on it. I have to crop and rotate these spreads to separate them. I will be doing this with multiple pdfs. This is why I need to get the file name of the input pdf, then append the corresponding number (1-12,2-11,3-10,4-9,5-8,6-7) all the while retaining the pdf extension. So my question is, how can I get the input’s file name and store that as a variable where I can later recall both it and the given number?

thanks for your help!

To get a file name, first you have to set the name to a variable (which “On open” does) and then tell Finder to get the name. The name does NOT include the extension, so you can just set name without worrying about the extension- very sensible if you ask me.

on open thisPDF
	tell application "Finder"
		set PDFname to name of thisPDF
		set PDFnum to "1-12"
		set name of thisPDF to (PDFname & " " & PDFnum)
	end tell
end open

And I may be being slow, but what do you mean by “Spreads” and what is the “corresponding number”?

I appreciate your help! That was what I was asking for. I’m pretty sure I know what I need to do to finish my script. Thanks!

You aren’t being slow at all. I don’t expect everyone to know printing/publishing terms. A spread is pretty much two pages placed together in one image. When I say corresponding number I am referring to 1-12,2-11,3-10,4-9,5-8,6-7, and those are the pages.

To explain even further, 1+12 is a spread with 2+11 on it’s reverse. This is wrapped around 3+10 (w/ 4+9 on the reverse). These are wrapped around 5+8 with 6+7 on it’s reverse. I imagine I didn’t make clear that when saying 1-12, I meant it as one and twelve (not one through twelve).

Thanks again :slight_smile:

edit: I got everything put in the script properly (I’m pretty sure) but I keep getting an error saying it can’t get the name. Am I likely doing something wrong, or are there limits to filenames it can read? I’m somewhat ashamed of my childish script and would rather not post it :confused: It’s written for an extremely specific situation.

Oh, that makes sense. If I understand you right, you want to do something that CocoaBooklet does. You might want to check it out, although after all this effort to make a solution yourself you might not want to ;)!