Hello all I am a new user to the Mac and loving it… and hoping to upgrade my skills starting small with scripting. Im reading thru the beginner pages and hoping someone can help me write a script file to rename files the way I want them in my DLNA server or if someone can walk me thru it.
Here is what I want
I want to rename a whole directory of say Criminal Mines
Now from what I been reading the filename is a string of characters and I need to get my script file to turn the string into a list of words then have it only take the infomation out of the list that I want but I think I am over thinking this problem and complicating it cause now Im thinking not all the titles such as “Aftermath” are going to be one word some times they are a dozen words…
I guess that there is faster scheme using regex but here is a script doing the job.
on run
local leDossier, lesNoms, unNom, uneListe, off7, aPiece, removable, nouveauNom
set leDossier to (path to desktop as text) & "themagician:"
tell application "System Events"
set lesNoms to name of disk items of folder leDossier whose visible is true
set unNom to item 1 of lesNoms
set uneListe to my decoupe(unNom, space)
set off7 to 0
repeat with aPiece in uneListe
if aPiece is "" then
set off7 to off7 + 1
else if character 1 of aPiece is not in "0123456789" then
set off7 to off7 + 1 + (count aPiece)
else
exit repeat
end if
end repeat
set removable to text 1 thru off7 of unNom
# set nouveauxNoms to {}
repeat with unNom in lesNoms
set nouveauNom to my remplace(unNom, removable, "S0")
set off7 to offset of "." in nouveauNom
set nouveauNom to (text 1 thru (off7 - 1) of nouveauNom) & "E" & text (off7 + 1) thru -1 of nouveauNom
set off7 to offset of space in nouveauNom
set nouveauNom to (text 1 thru off7 of nouveauNom) & "- " & text (off7 + 1) thru -1 of nouveauNom
set name of disk item (leDossier & unNom) to nouveauNom
# set end of nouveauxNoms to nouveauNom
end repeat
end tell
end run
#=====
on decoupe(t, d)
local oTIDs, l
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set l to text items of t
set AppleScript's text item delimiters to oTIDs
return l
end decoupe
#=====
on recolle(l, d)
local oTIDs, t
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set t to "" & l
set AppleScript's text item delimiters to oTIDs
return t
end recolle
#=====
(*
replaces every occurences of d1 by d2 in the text t
*)
on remplace(t, d1, d2)
local oTIDs, l
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d1}
set l to text items of t
set AppleScript's text item delimiters to d2
set t to "" & l
set AppleScript's text item delimiters to oTIDs
return t
end remplace
#=====
KOENIG Yvan (VALLAURIS, France) mercredi 2 octobre 2013 17:54:20
There is no need to read French.
I just used French words to name variables.
Naming one GWBusch, GWButcher, GWBoucher, Son_Of_A_Bitch, menteur, assassin or criminelDeGuerre would change nothing.
Naming one JCarter or CasseNoisettes changes nothing too.
leDossier means theFolder
lesNoms means theNames
unNom means aName
uneListe means aList
off7 is a phonetic way to say offset in French but don’t replace it by offset which is a reserved word
aPiece
removable
nouveauNom means newName
I’m French and for years, I use scripts posted here and there with English variables Names.
French is an official language in UNO, Olympic Games . so I feel free to post, from time to time, scripts using French variable names and will not edit them.
KOENIG Yvan (VALLAURIS, France) jeudi 3 octobre 2013 09:40:59
Ok thanks Yvan Koenig Im sorry for offending you I didn’t mean to I was just trying to get it easier to understand because I am just learning the scripting language
Don’t mention it. You’re not offending anyone, it’s an English forum and Yvan is here long enough to know that, especially with new AppleScripters, it’s better to post code with English variables. Another reason to use English in AppleScript is because the syntax itself uses english commands, classnames, controls and keywords too so it’s French combined. Yvan’s fault, not yours
Here another example, more a tutorial where line by line will be explained what the script does:
# choose a folder that contains files that needs to be processed
set theFolder to POSIX path of (choose folder)
# don't list files that doesn't meet the format; safes format check later
set theFileNames to every paragraph of (do shell script "ls " & quoted form of theFolder & " | egrep '[0-9]{1,2}\\.[0-9]{1,2}.*\\.avi$'")
repeat with fileName in theFileNames
# using do shell script here to rename the file; it's making the script relative slow
do shell script "mv " & quoted form of (theFolder & fileName) & space & quoted form of (theFolder & reformat(fileName))
end repeat
on reformat(buffer)
# reformat ^[:name of show:][0-9]{1,2}\.[0-9]{1,2}[:name of episode:]\.avi$
# into format ^S[0-9][0-9]E[0-9][0-9] - [:name of episode:]\.avi$
repeat
# get the index of the first number in the file name
set i to myOffsetOfFirstNumber(buffer)
# remove (part) name of the show
set buffer to text i thru -1 of buffer
# check if the next text is in format [0-9]{1,2}\.[0-9]{1,2}
# this check makes sure that the name of the show doesn't contain a number so it works with TV shows like 24, Hawaii Five-0 and 2 broke girls
if character 2 of buffer = "." and isNumber(character 3 of buffer) then
# format is x.x meaning we have removed the name of the show; continue
exit repeat
else if isNumber(character 2 of buffer) and character 3 of buffer = "." then
# format is xx.xx? meaning we have removed the name of the show; continue
exit repeat
end if
# if we ever get here, the number was part of the name of the show
# remove the number
set buffer to text 2 thru -1 of buffer
end repeat
# get the index of the period symbol in buffer
set i to myOffset(buffer, ".")
# character(s) until period are season; add a leading zero is length < 2
set season to text -2 thru -1 of ("00" & text 1 thru (i - 1) of buffer)
# remove season, including period, from the buffer
set buffer to text (i + 1) thru -1 of buffer
# get index of the first space in buffer
set i to myOffset(buffer, space)
# character(s) until space are epsiode; add leading zero is lengt is < 2
set episode to text -2 thru -1 of ("00" & text 1 thru (i - 1) of buffer)
# remaining text is name of the episode and extension of the file name; return new format
return "S" & season & "E" & episode & " - " & text (i + 1) thru -1 of buffer
end reformat
on myOffsetOfFirstNumber(str)
# A brute force attemp to get the index of the first number
# when text becomes larger (more than 16 characters) this attemp is faster than linear search
set lowestIndex to (count of str) + 1
repeat with num in {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
set i to myOffset(str, num)
if i is not 0 and lowestIndex > i then set lowestIndex to i
end repeat
if lowestIndex > (count of str) then set lowestIndex to 0
return lowestIndex
end myOffsetOfFirstNumber
on myOffset(str, c)
# A faster way of getting the index of an substring in a string (vs. offset command)
# the improvement depends on the version of AppleScript's that's being used
# Return the first index of character of the string or 0 like the offset command
set {oldTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, c}
set textItems to text items of str
set AppleScript's text item delimiters to oldTID
if (count of textItems) < 2 then return 0
return (count of item 1 of textItems) + 1
end myOffset
on isNumber(c)
# returns true when character is a number or false when not
return c is in "012345678"
end isNumber
on reformat(buffer)
# reformat ^[:name of show:][0-9]{1,2}.[0-9]{1,2}[:name of episode:].avi$
# into format ^S[0-9][0-9]E[0-9][0-9] - [:name of episode:].avi$
Thank-you DJ Bazzle Wazzle
I see in the above how the file in renamed… but I am still confused on how the script is breaking down the file name so it know exactly how many letters etc I can see is the above all ugh… not sure how to explain my confusing… Im sitting here wondering if I will ever be able to write my own scripts beside Hello world LOL Im going to read the script a few more times see if it sinks in I can sort of see what this is doing but still not seeing how it is knowing how to separate all the words and numbers
I understand it can be confusing but what I basically do is having a variable buffer in the beginning of the handler (function) then containing the complete file name. Then it removes all the characters until the number with a period; the name of the show has been removed from buffer. The code here (see: loop) seems complex but when a show name contains a number (without a period) the code still functions. Now store the season number in a variable season and remove the season from buffer. Do the same with the episode number. Then concatenate (merge) the strings “S”, season, “E”, episode, " - " and remaining text in buffer.
The last bit, remaining text in buffer, is a bit shortened because I start reading at the position behind the space after the episode number to save a few lines of code. Of course it could be shortened down even more but it would make the code only less understandable.
In most of my scripts, I use French varnames because it’s an efficient way to be quite sure that there will not be conflicts with reserved words.
According to your advice, I guess that Frenchies must translate their first and last names when they exchange with English users because the availability of French names in English sentences would make them meaningless.
If French varnames were a problem, it was easy to use Find/Replace to replace them by the English ones which I gave.
To make you happy, here is a new version using English varnames (borrowed from Lord Byron)
on run
local I_had_a_dream_which_was_not_all_a_dream, The_bright_sun_was_extinguished_and_the_stars, Did_wander_darkling_in_the_eternal_space, Rayless_and_pathless_and_the_icy_earth, Swung_blind_and_blackening_in_the_moonless_air, Morn_came_and_went_and_came_and_brought_no_day, And_men_forgot_their_passions_in_the_dread, Of_this_their_desolation_and_all_hearts
set I_had_a_dream_which_was_not_all_a_dream to (path to desktop as text) & "themagician:"
tell application "System Events"
set The_bright_sun_was_extinguished_and_the_stars to name of disk items of folder I_had_a_dream_which_was_not_all_a_dream whose visible is true
set Did_wander_darkling_in_the_eternal_space to item 1 of The_bright_sun_was_extinguished_and_the_stars
set Rayless_and_pathless_and_the_icy_earth to my And_they_did_live_by_watchfires --and_the_thrones(Did_wander_darkling_in_the_eternal_space, space)
set Swung_blind_and_blackening_in_the_moonless_air to 0
repeat with Morn_came_and_went_and_came_and_brought_no_day in Rayless_and_pathless_and_the_icy_earth
if Morn_came_and_went_and_came_and_brought_no_day is "" then
set Swung_blind_and_blackening_in_the_moonless_air to Swung_blind_and_blackening_in_the_moonless_air + 1
else if character 1 of Morn_came_and_went_and_came_and_brought_no_day is not in "0123456789" then
set Swung_blind_and_blackening_in_the_moonless_air to Swung_blind_and_blackening_in_the_moonless_air + 1 + (count Morn_came_and_went_and_came_and_brought_no_day)
else
exit repeat
end if
end repeat
set And_men_forgot_their_passions_in_the_dread to text 1 thru Swung_blind_and_blackening_in_the_moonless_air of Did_wander_darkling_in_the_eternal_space
# set Were_chilled_into_a_selfish_prayer_for_light to {}
repeat with Did_wander_darkling_in_the_eternal_space in The_bright_sun_was_extinguished_and_the_stars
set Of_this_their_desolation_and_all_hearts to my The_habitations_of_all_things_which_dwell(Did_wander_darkling_in_the_eternal_space, And_men_forgot_their_passions_in_the_dread, "S0")
set Swung_blind_and_blackening_in_the_moonless_air to offset of "." in Of_this_their_desolation_and_all_hearts
set Of_this_their_desolation_and_all_hearts to (text 1 thru (Swung_blind_and_blackening_in_the_moonless_air - 1) of Of_this_their_desolation_and_all_hearts) & "E" & text (Swung_blind_and_blackening_in_the_moonless_air + 1) thru -1 of Of_this_their_desolation_and_all_hearts
set Swung_blind_and_blackening_in_the_moonless_air to offset of space in Of_this_their_desolation_and_all_hearts
set Of_this_their_desolation_and_all_hearts to (text 1 thru Swung_blind_and_blackening_in_the_moonless_air of Of_this_their_desolation_and_all_hearts) & "- " & text (Swung_blind_and_blackening_in_the_moonless_air + 1) thru -1 of Of_this_their_desolation_and_all_hearts
set name of disk item (I_had_a_dream_which_was_not_all_a_dream & Did_wander_darkling_in_the_eternal_space) to Of_this_their_desolation_and_all_hearts
# set end of Were_chilled_into_a_selfish_prayer_for_light to Of_this_their_desolation_and_all_hearts
end repeat
end tell
end run
#=====
on And_they_did_live_by_watchfires_and_the_thrones(t, d)
local oTIDs, l
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set l to text items of t
set AppleScript's text item delimiters to oTIDs
return l
end And_they_did_live_by_watchfires_and_the_thrones
#=====
on The_palaces_of_crowned_kings_the_huts(l, d)
local oTIDs, t
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set t to "" & l
set AppleScript's text item delimiters to oTIDs
return t
end The_palaces_of_crowned_kings_the_huts
#=====
(*
replaces every occurences of d1 by d2 in the text t
*)
on The_habitations_of_all_things_which_dwell(t, d1, d2)
local oTIDs, l
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d1}
set l to text items of t
set AppleScript's text item delimiters to d2
set t to "" & l
set AppleScript's text item delimiters to oTIDs
return t
end The_habitations_of_all_things_which_dwell
#=====
KOENIG Yvan (VALLAURIS, France) dimanche 6 octobre 2013 14:38:49
The ideal is to make the scripting help you’re giving understandable to the people you’re trying to help. Since MacScripter’s fora use the English language, one has to accept that many posters won’t understand whatever other languages people happen to use at home. As far as I’m aware, there’s no ban on French variable names here; but there is a general programming etiquette that variables and handlers should have meaningful labels ” that is, labels conveying meaning to whoever has to understand the code. In an Anglophone community, that means “mainly English or English-like”.
You could have done that yourself instead of posting defiantly about your language’s status in the world.