• 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
Use Spotlight to Archive Files
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Use Spotlight to Archive Files


  • Subject: Use Spotlight to Archive Files
  • From: Christopher Stone <email@hidden>
  • Date: Tue, 11 Sep 2012 13:12:05 -0500

Hey Folks,

I've been meaning to write this forever and finally got around to it.

It take a single selected file in the Finder an uses Spotlight to attempt to locate similar files in my Sofware_Archive folder.  If found it will then attempt to move the file into that folder and reveal it in the Finder.

The script is not very smart so far as it only uses the first word of the selected file to find against.  That is proving to be a bit problematic, so I'll have to fix it soon.  I need to refresh my memory on how to emplace multiple query strings in mdfind before I can progress very far with that though.

I also want to be able to process more than one file at once (that's easy though).

If anyone has experience working with Spotlight from the command line I'd welcome their contributions.

I have a mess of stuff in my downloads folder to archive, and this script is already starting to pay dividends.  It's rapidly helping me to file stuff and find duplicates.

--
Best Regards,
Chris

------------------------------------------------------------------------------------------------
#       Author: Christopher Stone <email@hidden>
#      Created: 2012-09-11 : 12:00
#     Modified: 2012-09-11 : 12:32 
#  Application: Finder & mdfind commandline (Spotlight)
#      Purpose: Find similar file to Finder-selection in software archive and move selection to it.
# Dependencies: None: only OSX components used.
------------------------------------------------------------------------------------------------
try

  

  # Several of these are unnecessary for *this* script but here for convenience.
  set applicationFolder to (POSIX path of (path to applications folder))
  set userAppSupportFolder to quoted form of (POSIX path of (path to application support from user domain))
  set userLibraryFolder to quoted form of (POSIX path of (path to library folder from user domain))
  set userPrefsFolder to quoted form of (POSIX path of (path to preferences folder from user domain))
  set softwareArchiveFolder to quoted form of (POSIX path of ("" & (path to home folder) & "Software_Archive:"))

  

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

  

  set _word to word 1 of _name
  set searchString to "*" & _word & "*"
  set searchFolder to 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
      # set bounds of front window to {0, 44, 870, 520}
      move _file to _folder
      reveal _file
      activate
    end tell
  end if

  

on error e number n
  set e to "Error: " & e & return & return & "Error Number: " & n
  beep
  tell me to set dDlg to display dialog e 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

  • Follow-Ups:
    • Re: Use Spotlight to Archive Files
      • From: Christopher Stone <email@hidden>
    • Re: Use Spotlight to Archive Files
      • From: Axel Luttgens <email@hidden>
  • Prev by Date: Re: Script Debugger 5: Duplicate Selection Handler
  • Next by Date: Re: Script Debugger 5: Duplicate Selection Handler
  • Previous by thread: Re: Rewrite simple Finder tell statement to "do shell script"
  • Next by thread: Re: Use Spotlight to Archive Files
  • Index(es):
    • Date
    • Thread