C-Command Software Forum

Assistance with scripting (sorting)

Some previous discussions have pointed out that it’s possible to create custom sort keys by putting the information in <Title> <From> or other fields. I’m using that method, but I only figured it out after I created several thousand entries.

So, I’m looking for someone to write a script to rearrange some of my data.
The script is to transfer some information from the “Title” field to the “From” field.
Example: Current title is: * IBM 359 blah blah 2006-02*

The script should take the last 7 (or 8) characters from the title, and move them into the From field. A fancier version of this would look for a string of the form “200n-nn” and move that string, wherever it appears in the title field. It sounds easy, but I’ve never worked with Applescript. The closest I could find to the right script is below. I can pay for this - probably the easiest is to quote me an hourly rate and a time estimate. Thanks!

Script
tell application “EagleFiler”
display dialog “Enter new From:” default answer “”
set _newFrom to text returned of the result
set _records to selected records of browser window 1
repeat with _record in _records
set _record’s from name to _newFrom
end repeat
end tell

(This script comes from the EF web site. It takes user input to make a mass-change to multiple From entries.)

Here’s a script to do this:

tell application "EagleFiler"
    set _records to selected records of browser window 1
    repeat with _record in _records
        set _dateAndBase to my findDateAndBase(_record's title)
        if _dateAndBase is not {} then
            set _record's from name to item 1 of _dateAndBase
            set _record's title to item 2 of _dateAndBase
        end if
    end repeat
end tell

on findDateAndBase(_string)
    set _setup to "import re, sys;"
    set _setString to "s = unicode(sys.argv[1], 'utf8');"
    set _setPattern to "pattern = u'200\\d-\\d\\d';"
    set _setMatch to "match = re.search(pattern, s);"
    set _printMatch to "print match.group(0);"
    set _printBase to "print re.sub(pattern, '', s, 1).strip().encode('utf8');"
    set _pythonScript to _setup & _setString & _setPattern & _setMatch & _printMatch & _printBase
    set _shellScript to "python -c " & _pythonScript's quoted form & " " & _string's quoted form
    try
        set _output to do shell script _shellScript
        set AppleScript's text item delimiters to return
        return text items of _output
    on error
        return {}
    end try
end findDateAndBase