• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: script to rename files (2)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: script to rename files (2)


  • Subject: Re: script to rename files (2)
  • From: email@hidden
  • Date: Wed, 7 Aug 2002 10:30:08 -0700

Here's my humble addition to your options; this code will let you read an
INI file in and then query it for key value pairs. Combined with a folder
action (such as the one below that I just compiled in my email client) and
you should be good to go.

on adding folder items to theFolder after receiving theFiles
set fileReader to load script file "path/to/file/below.scpt"
tell fileReader
init("path/to/common/script")
open "path/to/mapping/INI"
end tell

repeat with aFile in theFiles
tell app "Finder" to set oldName to name of aFile
tell fileReader to set newName to keyForValue(oldName)

tell app "Finder" to set name of aFile to newName
end repeat
end adding folder items to

-----------------------------------------
(* A Script way to look at a File *)

property keys : {}
property values : {}
property pathToFile : ""
property common : ""
property scriptFolder : ""

to init(inScriptFolder)
log "Value List is InitializingI"
set scriptFolder to inScriptFolder

set pathToFile to scriptFolder & "Common.scpt"
set common to load script file pathToFile
end init

on open theFile
log "Opening '" & theFile & "'I"
try
set fileRef to open for access theFile
set linefeed to ASCII character 10

set theLines to read fileRef B
using delimiter {linefeed}
log theLines

repeat with i from 1 to count of theLines
with transaction "Add To Cache"
set thisLine to item i of theLines

tell common
set thisLine to trim from thisLine
end tell

try
if thisLine contains "=" then
tell common
set components to allTextItems
out of thisLine between {"="}

set thisKey to trim from (item 1
of components) as Unicode text
set thisValue to trim from (item
2 of components) as Unicode text
end tell

--and pop it onto the lists
copy thisKey to end of keys
copy thisValue to end of values
log "loop iteration: " & i
log "thisKey: " & thisKey
log "thisValue: " & thisValue
end if
on error errMsg
--we might not have componants, so we catch
that here.
log "invalid line: " & return & thisLine &
return & errMsg
end try
end transaction
end repeat

display()

close access fileRef
on error errMsg
error "Cannot open file: " & errMsg
end try
end open

on valueForKey(theKey)
try
--let's put this into a format we can be sure of
set theKey to theKey as Unicode text

--now let's display debug info
tell common to set theValuesString to stringFormat of it for
keys between ", "
--log "Request for: " & theKey & " in" & return &
theValuesString

repeat with i from 1 to count of keys
ignoring punctuation and white space
set thisKey to item i of keys as Unicode text

(*log thisKey & "==" & theKey & return B
& (thisKey is theKey)*)

if thisKey is theKey then
set retval to item i of values
--log "Found: " & retval & " (" & item i of
values & ")"
return retval
end if
end ignoring
end repeat

log "Not found. Returning NULL"
return "NULL"
on error errMsg number errNum
error "ValueForKey error: " & return & errMsg
end try
end valueForKey

to display()
log "***Current Key/Value Pairs******************"
repeat with eachItem in keys
log eachItem & " is " & valueForKey(eachItem)
end repeat
log "***************************************" as text
end display

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Seth A. Roby
Scriptwriter Intern
"Life is like an exploded clown. It's really funny until you figure out
what just happened."


> [Muttering darkly about snotty list depriving poor readers of my many
gems
> of wisdom, just because I go a few (dozen) KB over maximum length on
> occasion...;p]
>
> p.s. I cooked up the following bit of code for my own amusement, but
> someone out there might find it useful. It turns a tab-delimited table
> string into an AS object; you can then query each line in turn by calling
> the object's nextEntry() method.
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.

  • Prev by Date: Re: putting a partition away in OSX
  • Next by Date: Re: Atomic scripts
  • Previous by thread: Re: script to rename files (2)
  • Next by thread: Re: script to rename files (2)
  • Index(es):
    • Date
    • Thread