Sampathr100

Q: Apple Script and Automator: To find filepaths in long text

Hello,

 

I would like to create a service,

 

step 1: Identify multiple file paths in the long text.

step 2: Get finder items action.

step 3: List to user the file names, and wait for user action item (for yes or no).

step 4: move finder items to trash action.

 

I am stuck up at step 1: how to retrieve the filepaths in the given text. (multiple file paths found). also please help me how to show the useraction item, so user can select yes or no.

 

Thanks
SampathR

MacBook Pro, Mac OSX 11

Posted on Sep 20, 2016 7:46 AM

Close

Q: Apple Script and Automator: To find filepaths in long text

  • All replies
  • Helpful answers

  • by rccharles,

    rccharles rccharles Sep 20, 2016 9:20 AM in response to Sampathr100
    Level 6 (8,516 points)
    Classic Mac OS
    Sep 20, 2016 9:20 AM in response to Sampathr100

    This will depend on what is in the string.  please post  examples of the string you wish to parse.

     

    R

  • by Sampathr100,

    Sampathr100 Sampathr100 Sep 20, 2016 11:29 AM in response to Sampathr100
    Level 1 (4 points)
    Mac OS X
    Sep 20, 2016 11:29 AM in response to Sampathr100

    Thanks much.

     

    example:

    "

    ttest string xyzzzz

     

    Error in /usr/local/work/…./test.java

    Error in /user/local/work/…/test2.java

    Error in /usr/local/work/…/test3.java

     

    "

    if i run the script, i should get the full paths in the above paragraph.

     

     

    /usr/local/work/…/test.java

    /usr/local/work/…./test2.java

    /usr/local/work/…./test3.java

  • by Camelot,

    Camelot Camelot Sep 20, 2016 5:28 PM in response to Sampathr100
    Level 8 (47,300 points)
    Mac OS X
    Sep 20, 2016 5:28 PM in response to Sampathr100

    Are all the paths in that specific format? if so, this is very easy to do with vanilla AppleScript, which may be easier than an Automator workflow. Just paste the following into a new Script Editor document and click run:

     

    use AppleScript version "2.4" -- Yosemite (10.10) or later

    use scripting additions

     

    set fileContents to paragraphs of (read (choose file))

     

    repeat with eachLine in fileContents

      if (eachLine as text) starts with "Error in " then

      set filePath to characters 10 through end of eachLine as text

      set myDialog to display dialog "Do something with '" & filePath & "'?" buttons {"No", "Yes"} default button 2

      if button returned of myDialog is "Yes" then

      -- do something if the user clicks 'yes', e.g.:

      tell application "Finder" to delete POSIX file filePath

      else

      --do something if the user clicks "no"

      end if

      end if

    end repeat


    Edit the script as necessary, specifically with the actions based on whether the user clicks yes or no.