New user, request for help with text manipulation in Bruji's BookPedia

Here a more traditional (but presumably faster) approach with UI scripting (since Bookpedia doesn’t provide any AppleScript dictionary):

Script:

set {saveTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {":"}}

# Max. number of records to be processed with one run
set maxEntries to 3

tell application "System Events"
  repeat with i from 1 to maxEntries
    tell application process "Bookpedia"
      tell window "Bookpedia"
        tell splitter group 1
          tell splitter group 2
            tell group 1
              tell scroll area 1
                tell table 1
                  tell row 1
                    tell text field 1
                      try
                        set oldTitle to its value
                        set newTitle to text item 1 of oldTitle
                        set newTitle to my trimLeadingSpaces(newTitle)
                        set originalTitle to text items 2 through -1 of oldTitle as text
                        set originalTitle to my trimLeadingSpaces(originalTitle)
                      on error
                        set originalTitle to ""
                      end try
                      perform action "AXShowMenu"
                      delay 0.1
                    end tell
                  end tell
                  click menu item "Edit…" of menu 1
                end tell
              end tell
            end tell
          end tell
        end tell
      end tell
      tell window oldTitle
        tell group 1
          tell scroll area 1
            delay 0.1
            set value of text field 1 to newTitle
            if originalTitle is not "" then set value of text field 3 to originalTitle
          end tell
        end tell
        tell button "OK" to perform action "AXPress"
      end tell
    end tell
  end repeat
end tell

set AppleScript's text item delimiters to saveTID

on trimLeadingSpaces(str)
  repeat with i from 1 to count of str
    if str begins with space then
      set str to text 2 through -1 of str
    else
      exit repeat
    end if
  end repeat
  return str
end trimLeadingSpaces

Macro with the script:

[temporarily removed as we are debugging]

With this example dataset:

…it returns this…


It is required that in the editor window the “Original Title” is the third text field, like this:

48-pty-fs8

More Notes:

  • In the current test form the script only runs on the first three entries. You can change this in the AppleScript (“maxEntries” variable.)

  • Currently the script only removes leading spaces for each of the re-defined fields. (Trailing spaces usually do no harm, but this can be implemented too.)

  • When you test the script, make sure the Bookpedia window is open.

  • The screen position of the windows doesn’t matter at all.

  • If you get an error like “Can’t get menu item…” try to uncomment the delay 0.2 (It’s the only commented line in the script.)

  • The script is meant as a test: if it basically works for you (on a couple of entries), then we can still refine it. (I have no clue what a window with 8000 records looks like, or if your database is somehow setup differently. I just downloaded the app and used the default setup.)


Edit 2019-08-20:

Updated script and macro, see also here.

Edit 2019-08-21T00:17+02:

  • Updated script
  • Removed macro download link (for the time of the debugging stage)

Edit 2019-08-21T02:43+02:

Updated script:

  • Colon-separated text items after the first colon go all into Original Title (leaving the colons in that part untouched)
  • Added some delays
  • No longer overwrites Original Title if we don’t have a value for it. (I know, you were OK with overwriting, but it is better this way, in case you are accidentally processing records you already had processed before. No data loss.)
2 Likes