First of all I want to state that I’m new to the forum, Applescript and scripting in general. So I’m a real noob.
In the last couple of days I’m trying to write this Apple script which locates a string and replaces it with a new one BUT with incrementing numbers in this certain format.
OK this is what I can come up with so far:
set thefile to “slugslugslug”
display dialog “Scene name?” default answer “”
set sceneName to text returned of result
set sceneNumber to 1
set newName to sceneName & “_00” & sceneNumber
set replaceItWith to “” & newName & “”
findAndReplace(“slug”, replaceItWith, thefile)
on findAndReplace(tofind, toreplace, TheString)
set ditd to text item delimiters
set res to missing value
set text item delimiters to tofind
repeat with tis in text items of TheString
if res is missing value then
set res to tis
else
set res to res & toreplace & tis
end if
end repeat
set text item delimiters to ditd
return res
end findAndReplace
which returns the text as: “ja01_001ja01_001ja01_001”
i want the script to return: “ja01_001ja01_002ja01_003”
The created text in the beginning is just momentary as in the future the script has to read the text from a Final Cut XML document.
Problems:
As this findAndReplace function find and replace a certain string in one go, with this script as it is I don’t know how to increment the shot number.
Right now I get the _00x extension in quite a problematic way (set newName to sceneName & “_00” & sceneNumber). As soon as the number gets to double digits it starts to create _0017 sort of format which is wrong. I has to be _017. So is there a way to make Applescript add two zeros in front of its counter?
I appreciate any help as if I can make this work it really is going to ease my life at work .
Cheers
SB
Model: Mac Pro
AppleScript: 2.2.1
Browser: Firefox 3.0.4
Operating System: Mac OS X (10.5)
set thefile to "<name>slug<name/><name>slug<name/><name>slug<name/>"
set tofind to "slug"
set newname to "Hello_"
set chars to length of tofind
set n to 1
set theoffset to offset of tofind in thefile
repeat while theoffset > 0
set nchar to newname & text -2 thru -1 of ("0" & (n as text))
set thefile to text 1 thru (theoffset - 1) of thefile & nchar & text (theoffset + chars) thru -1 of thefile
set n to n + 1
set theoffset to offset of tofind in thefile
end repeat
thefile
on ReplaceWithIndexedValue(startText, textToReplace, indexPrefix, startingIndex)
set AppleScript's text item delimiters to {textToReplace}
set subStrings to text items of startText as list
set AppleScript's text item delimiters to {ASCII character 5}
set cleanDelimiters to subStrings as string
set i to (startingIndex - 1)
repeat until (count of text items of cleanDelimiters) = 1
set i to i + 1
set cleanDelimiters to (item 1 of text items of cleanDelimiters) ¬
& (indexPrefix & i as text) ¬
& rest of text items of cleanDelimiters as string
end repeat
return cleanDelimiters
end ReplaceWithIndexedValue
--BEGIN demo
set testTxt to "aXbXcXdXeXfXgXhXiXjXk"
set testReplace to "X"
set testWith to "Y-"
display dialog ReplaceWithIndexedValue(testTxt, testReplace, testWith, 1)
--aY-1bY-2cY-3dY-4eY-5fY-6gY-7hY-8iY-9jY-10k
display dialog ReplaceWithIndexedValue("aaXbbXccX", "X", " X-", 3)
-- aa X-3bb X-4cc X-5
Here’s a more refined version of my script that shouldn’t have a problem with matches at the beginning or end of thefile:
set thefile to "<name>slug<name/><name>slug<name/><name>slug<name/>"
set tofind to "<name>slug<name/>"
set newname to "Hello_"
set chars to count of tofind
set thefilelen to count of thefile
set n to 1
set theoffset to offset of tofind in thefile
repeat while theoffset > 0
set nchar to newname & text -2 thru -1 of ("0" & (n as text))
if theoffset > 1 and (theoffset + chars) < thefilelen then
set thefile to text 1 thru (theoffset - 1) of thefile & nchar & (text (theoffset + chars) thru -1 of thefile)
else
if theoffset = 1 then
set thefile to nchar & text (theoffset + chars) thru -1 of thefile
else
if (theoffset + chars) ≥ thefilelen then
set thefile to text 1 thru (theoffset - 1) of thefile & nchar
end if
end if
end if
set n to n + 1
set thefilelen to count of thefile
set theoffset to offset of tofind in thefile
end repeat
thefile