Applescript in Automator - how to accept input?

Discussion in 'Mac Programming' started by open0source, May 24, 2009.

  1. macrumors newbie

    Joined:
    Oct 17, 2008
    #1
    I'm a gigantic newbie to Applescript and am trying to write (what I think would be) a pretty simple script for a workflow. Basicly, the standard "Set Spotlight Comments For Finder Items" function, but I want the comments that are already on the file to show up in the entry box. What it's supposed to look like here:

    http://i271.photobucket.com/albums/jj149/open0source/Picture2-2.png

    But with the actual filename replaced by a variable of the path of the file that is being opened/dropped-onto the workflow. Any guidance is greatly appreciated. :)


    (Edit)

    This seems to work for getting the comments from a dropped file into a dialog box:

    on open droppedItem
    set itemPath to POSIX path of droppedItem
    set itemComments to do shell script "mdls -name kMDItemFinderComment " & itemPath
    display dialog itemComments
    end open

    but I can't use it as the workflow's script (replacing "display dialog" with "return")

    (Edit 2)

    Ditched automator, got a bare-bones-version working

    on open droppedItems
    try
    set checkForMultipleItems to second item of droppedItems
    on error
    --Do nothing
    end try
    set theFile to first item of droppedItems
    tell application "Finder"
    set currentComments to (get comment of theFile)
    set title to "Spotlight Comments:"
    set r to display dialog title default answer currentComments
    end tell
    end open

    (Sorry for wasting anyone's time)

    I'll update one more time when the entire thing is done, in case someone would like the final script.

    (Edit 3)
    Done,

    on open droppedItems
    try
    set checkForMultipleItems to second item of droppedItems
    on error
    --Do nothing
    end try
    set theFile to first item of droppedItems
    tell application "Finder"
    set currentComments to (get comment of theFile)
    set title to "Spotlight Comments:"
    set r to display dialog title default answer currentComments buttons {"Cancel", "Write"} default button 2
    set newComments to text returned of r
    if button returned of r is "Write" then
    set comment of theFile to newComments
    else
    set comment of theFile to currentComments
    end if
    end tell
    end open
     
  2. macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #2
    Newbie? Nice work.

    Just one question: Why do you have the try ... on error ... block? I left it out here, and offered a way to do multiple files with one drop.

    Code:
    on open droppedItems
        set numItems to the count of droppedItems
        -- this will handle multiple items
        tell application "Finder"
            repeat with x from 1 to numItems
                set theFile to item x of droppedItems
                set theName to the name of theFile -- this will help user know which file s/he's commenting
                set currentComments to (get comment of theFile)
                set title to "Spotlight Comments of " & quote & theName & quote
                set r to display dialog title default answer currentComments buttons {"Cancel", "Write"} default button 2
                set newComments to text returned of r
                if button returned of r is "Write" then
                    set comment of theFile to newComments
                else
                    set comment of theFile to currentComments
                end if
            end repeat
        end tell
    end open
    There's one "bug." The user might think "Cancel" will skip a file, when here it exits the script. Might make sense to have a three button dialog: Cancel, Skip & Write.

    Nice bit of work for a noob.

    mt
     
  3. macrumors newbie

    Joined:
    Dec 4, 2010
    #3
    Hi, I am new here.
    How can I change following script to work by double clicking instead of dropping files onto it?

    Code:
    on open droppedItems
        set numItems to the count of droppedItems
        -- this will handle multiple items
        tell application "Finder"
            repeat with x from 1 to numItems
                set theFile to item x of droppedItems
                set theName to the name of theFile -- this will help user know which file s/he's commenting
                set currentComments to (get comment of theFile)
                set title to "Spotlight Comments of " & quote & theName & quote
                set r to display dialog title default answer currentComments buttons {"Cancel", "Write"} default button 2
                set newComments to text returned of r
                if button returned of r is "Write" then
                    set comment of theFile to newComments
                else
                    set comment of theFile to currentComments
                end if
            end repeat
        end tell
    end open
    thank you
     

Share This Page