[Python] Return Capitalized String But Ignore Specified List of Common Words (a, and the,...

Discussão em 'Python' iniciado por Stack, Janeiro 25, 2025.

  1. Stack

    Stack Membro Participativo

    This Applescript capitalizes multiple listing title textfield within GarageSale.

    The program's built-in menu requires you to manually select one at a time and use the menu selection Edit > Transformations > Capitalize.

    This built-in menu options and the code below capitalizes a number of common words that should not be capitalized.

    How can I modify the code below to ignore code below to have it ignore a specified list of word when it capitalizes the title? The following list, which I've included in a variable, may change later.

    I am using the shell script because it does not mess up non alphabetical characters as the titles might include characters like hyphens, commas and colons.

    set DoNotCapList to {"a", "and", "as", "at", "but", "by", "down", "for", "from", "if", "in", "into", "like", "near", "nor", "of", "off ", "on", "once", "onto", "or", "over", "past", "so", "than", "that", "to", "upon", "when", "with", "yet"}



    tell application "GarageSale 9.8.1"
    repeat with theListing in (get selected ebay listings)
    set des to get the title of theListing
    set the title of the theListing to my titlecase(des)
    end repeat
    end tell

    on titlecase(txt)
    return do shell script "python -c \"import sys; print unicode(sys.argv[1], 'utf8').title().encode('utf8')\" " & quoted form of txt
    end titlecase

    Continue reading...

Compartilhe esta Página