You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Script for batch renaming files based on kMDItemWhereFroms Spotlight metadata

I've googled and searched all day in script forums and could not find a script that could do this. I grabbed pieces from a few scripts I found in forums and tried to make one without applescript knowledge. I managed to rename one file based on the "Where From" in the Finder "Get Info" window. But obviously, the script is faulty. I am hoping someone can help me sort it out. I need to batch rename hundreds of files with the URL in the "Where From" info. Here's the script.


-- get Spotlight kMDItemWhereFroms attribute


property getSpecific : true-- show the specific download URL?


set theFile to (choose file)

set aFile to quoted form of POSIX path of theFile

set theURL to (do shell script "mdls -name kMDItemWhereFroms -raw " & aFile)

if theURL is "(null)" then -- no attribute

set theURL to "(URL info not found)"

set theMessage to ""

else

if getSpecific then -- get the first item (the download URL)

set theURL to paragraph 2 of theURL

set here to offset of "\"" in theURL

set theURL to text (here + 1) thru -3 of theURL -- the download URL

tell application "Finder"

set name of theFile to theURL--I have no idea what im doing

end tell

else -- get the last item (the page/site URL)

set theMessage to "page/site "

set theURL to paragraph -2 of theURL

set here to offset of "\"" in theURL

set theURL to text (here + 1) thru -2 of theURL

end if

end if

Posted on Aug 30, 2012 7:42 PM

Reply
5 replies

Aug 31, 2012 7:48 AM in response to walazzo

Hi,


You cannot rename a file if the new name contains colon characters,

So, the script replace the colon character in the URL by the minus sign.


Here is the script :

----------------------------------------

propertygetSpecific : true -- show the specific download URL?


ifgetSpecificthen -- get the second line (the download URL)

set sedCMD to " | sed -n '2!d;s/:/-/g;s/.*\"\\(.*\\)\".*/\\1/p'"

else -- get the third line (the page/site URL)

set sedCMD to " | sed -n '3!d;s/:/-/g;s/.*\"\\(.*\\)\".*/\\1/p'"

endif

-- sed return "" if the mdls command return "(null)", sed return the text between double quotes


settheseFilestochoose filewithmultiple selections allowed

repeatwiththeFileintheseFiles

setaFiletoquoted formofPOSIX pathoftheFile

set theURL to do shell script "mdls -name kMDItemWhereFroms -raw " & aFile & sedCMD

if theURL is not "" then try -- not "(null)"

tell application "System Events" to set name of theFile to theURL

end try

endrepeat

----------------------------------------

Sep 2, 2012 9:43 AM in response to Jacques Rioux

ugh. sed has its place in the world, but there's almost never an excuse to use it from applescript. try this instead:


-- get Spotlight kMDItemWhereFroms attribute


property getSpecific : true-- show the specific download URL?


set theFile to (choose file)

set aFile to quoted form of POSIX path of theFile

set theURL to (do shell script "mdls -name kMDItemWhereFroms -raw " & aFile)

if theURL is "(null)" then -- no attribute

set theURL to "(URL info not found)"

set theMessage to ""

else

if getSpecific then -- get the first item (the download URL)

set theURL to paragraph 2 of theURL

set urlBits to tid(theURL, "\\")

set theURL to item 2 of urlBits

set theURL to tid(tid(theURL, ":"), "_") -- swaps colons for underscores

tell application "Finder"

set name of theFile to theURL--I have no idea what im doing

end tell

else -- get the last item (the page/site URL)

set theMessage to "page/site "

set theURL to paragraph -2 of theURL

set here to offset of "\"" in theURL

set theURL to text (here + 1) thru -2 of theURL

end if

end if


on tid(input, delim)

set {oldTID, my text item delimiters} to {my text item delimiters, delim}

if class of input is list then

set output to input as text

else

set output to text items of input

end if

set my text item delimiters to oldTID

return output

end tid

Sep 2, 2012 3:08 PM in response to twtwtw

Hi,


twtwtw wrote:


ugh. sed has its place in the world, but there's almost never an excuse to use it from applescript.

I respect your opinion.


My opinion is different, if you already use a shell from AppleScript, you can use other commands in this shell.


If the user wants to learn AppleScript, it can also learn shell commands.


So here is the script that uses three commands in a do shell script :

----------------------------------

propertygetSpecific : true -- show the specific download URL?


ifgetSpecificthen -- get the second line (the download URL)

set sedCMD to " | sed -n '2!d;s/\\//:/g;s/.*\"\\(.*\\)\".*/\\1/p'"

else -- get the third line (the page/site URL)

set sedCMD to " | sed -n '3!d;s/\\//:/g;s/.*\"\\(.*\\)\".*/\\1/p'"

endif


settheseFilestochoose filewithmultiple selections allowed

repeatwiththeFileintheseFiles

setaFiletoquoted formofPOSIX pathoftheFile

do shell script "f=" & aFile & ";url=$(mdls -name kMDItemWhereFroms -raw \"$f\"" & sedCMD & "); if [ ! -z \"$url\" ]; then mv \"$f\" \"${f%/*}/$url\";fi"

endrepeat

----------------------------------

The mv commnad rename the file.

sed replace slash character by a colon character, because slash character is a reserved character in Unix path, but the file will be renamed correctly because the shell replace the colon characters in each path by slash characters.

Sep 2, 2012 6:10 PM in response to Jacques Rioux

Jacques Rioux wrote:


do shell script "f=" & aFile & ";url=$(mdls -name kMDItemWhereFroms -raw \"$f\"" & sedCMD & "); if [ ! -z \"$url\" ]; then mv \"$f\" \"${f%/*}/$url\";fi"


Seriously? This reads like mud, and requires a strong understanding of unix to even begin to interpret. And yes, I know this because I have a strong understanding of unix, and trying to read through that mess gives me a headache. What's it look like to a newb? The applescript is far easier.


But whatever, I'm not going to quibble over this with you. As you say, you have your opinion and I have mine.

Script for batch renaming files based on kMDItemWhereFroms Spotlight metadata

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.