• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Use Spotlight to Archive Files
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Use Spotlight to Archive Files


  • Subject: Re: Use Spotlight to Archive Files
  • From: Christopher Stone <email@hidden>
  • Date: Wed, 19 Sep 2012 21:13:48 -0500

Hey Folks,

A few modifications.

CHANGES:
  + Allowed for files with a single word name.
  + Added a handler for 'quoted form of'.
  + Made sed script more readable.

--
Chris

------------------------------------------------------------------------------------------------
#       Author: Christopher Stone <email@hidden>
#      Created: 2012-09-11 : 12:00
#     Modified: 2012-09-19 : 16:49
#  Application: Finder & mdfind commandline (Spotlight)
#      Purpose: Find similar file to Finder-selection in software archive and move selection to it.
# Dependencies: Only OSX components used.
#      Version: 1.0
------------------------------------------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------------------------------------------
on q(_str)
  return quoted form of _str
end q
------------------------------------------------------------------------------------------------
--» MAIN
------------------------------------------------------------------------------------------------

try

  

  set softwareArchiveFolder to (POSIX path of ("" & (path to home folder) & "Software_Archive:"))

  

  tell application "SystemUIServer"
    activate
  end tell

  

  tell application "Finder"
    activate
    set sel to selection as alias list
    if sel ≠ {} then
      set _file to first item of sel
      set _name to name of _file
      set _ext to name extension of _file
    end if
  end tell

  

  if _ext ≠ "" then
    set AppleScript's text item delimiters to "." & _ext
    set _name to text item 1 of _name
  end if

  

  set _name to do shell script "echo " & q(_name) & " |
    sed -E '
      s/ *[0-9\\._-]+ */ /g
      s/ +$//
      s/^ +//;s/ +/*/g
  '"

  

  if length of (words of _name) = 1 then
    set _name to _name & " "
  end if

  

  set searchString to "*" & _name & "*"
  set searchFolder to quoted form of softwareArchiveFolder
  set cmd to "mdfind -onlyin " & searchFolder & " \"kMDItemFSName == '" & searchString & "'c\""
  set foundItems to do shell script cmd

  

  set _folder to false

  

  if foundItems ≠ "" then
    set foundItems to paragraphs of foundItems
    set AppleScript's text item delimiters to "/"
    repeat with i in foundItems
      if i contains "." then
        set _folder to contents of i
        set _folder to (text items 1 thru -2 of _folder) as text
        set _folder to alias POSIX file _folder
        exit repeat
      end if
    end repeat
  end if

  

  if _folder ≠ false then
    tell application "Finder"
      open _folder
      move _file to _folder
      reveal _file
    end tell
  else
    set sel to (item 1 of sel) as «class furl»
    sel
    set the clipboard to sel
    set fldr to alias POSIX file softwareArchiveFolder
    tell application "Finder" to open fldr
  end if

  

on error e number n
  set e to e & return & return & "Num: " & n
  tell me to set dDlg to display dialog e with title "ERROR!" buttons {"Cancel", "Copy", "OK"} default button "OK"
  if button returned of dDlg = "Copy" then set the clipboard to e
end try

------------------------------------------------------------------------------------------------

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

References: 
 >Use Spotlight to Archive Files (From: Christopher Stone <email@hidden>)
 >Re: Use Spotlight to Archive Files (From: Christopher Stone <email@hidden>)
 >Re: Use Spotlight to Archive Files (From: Christopher Stone <email@hidden>)

  • Prev by Date: ASE and 10.8.2
  • Next by Date: Re: ASE and 10.8.2
  • Previous by thread: Re: Use Spotlight to Archive Files
  • Next by thread: error number -1799 and good results?
  • Index(es):
    • Date
    • Thread