• 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
File Location by Reference
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

File Location by Reference


  • Subject: File Location by Reference
  • From: Christopher Stone <email@hidden>
  • Date: Fri, 12 Apr 2013 15:41:42 -0500

Hey Folks,

When I completely rebuilt my system from scratch on Mountain Lion I had a lot of problems with scripts where I had been lazy used hard-coded paths.

I've known for well over a decade that the most sensible way to write a reusable file path in Applescript is to use a reference as an anchor if possible.

Example:

alias ((path to home folder as text) & "test_directory:shell:test.sh")

After my experience with Mountain Lion I decided to get really diligent about using referenced-paths, but I got really tired of writing them out by hand — so of course I wrote a script.

This one depends upon FastScripts as the script runner and to work around the (Mountain)Lion Finder-selection bug, but you can easily change that to suit your needs.  (Look for ••••• in the script.)

It only returns a reference for a single item (or the first item of a selection), but that's easy enough to change as well.

--
Best Regards,
Chris

------------------------------------------------------------------------------------------------
# Auth: Christopher Stone <email@hidden>
# dCre: 2012-09-22 : 00:36
# dMod: 2012-10-26 : 00:23
# Appl: Finder
# Task: Create path to file by reference; copy ref-string to the Clipboard.
# Deps: Uses only OSX components.
# Tags: @Applescript, @Finder, @Alias, @Path, @Reference
------------------------------------------------------------------------------------------------

try

  

  set expandedPathTo to {}
  set pathToSpec to {application support, applications folder, desktop, desktop pictures folder, documents folder, downloads folder, favorites folder, Folder Action scripts, fonts, help, home folder, internet plugins, keychain folder, library folder, modem scripts, movies folder, music folder, pictures folder, preferences, printer descriptions, public folder, scripting additions, scripts folder, shared documents, shared libraries, sites folder, startup disk, startup items, system folder, system preferences, temporary items, trash, users folder, utilities folder, workflows folder}

  

  repeat with i in pathToSpec
    set end of expandedPathTo to path to (get contents of i) from user domain
  end repeat

  

  set AppleScript's text item delimiters to return
  set expandedPathTo to text items of (expandedPathTo as text)

  

  # Work-around for (Mountain) Lion Finder selection bug.
  # Use the 'SystemUIServer' if you don't have FastScripts.
  tell application "FastScripts"
    activate --» •••••
  end tell
  tell application "Finder"
    activate --» •••••
    set sel to selection as alias list
    if sel ≠ {} then
      set f to first item of sel as text
    end if
  end tell

  

  set AppleScript's text item delimiters to ":"
  set _len to (length of (text items of f))

  

  repeat with i from _len to 1 by -1
    set _path to ((text items 1 thru i of f) as text) & ":"
    if _path is in expandedPathTo then
      repeat with idx from 1 to (length of expandedPathTo)
        if (item idx of expandedPathTo) = _path then
          set refFldr to "path to " & (item idx of pathToSpec) as text
          set diskLocalRef to ("alias " & ("(" & "(" & refFldr & " as text) & ") & "\"" & (text items (i + 1) thru -1 of f) as text) & "\")"

          

          try
            run script diskLocalRef
          on error
            set refFldr to refFldr & " from user domain"
            set diskLocalRef to ("alias " & ("(" & "(" & refFldr & " as text) & ") & "\"" & (text items (i + 1) thru -1 of f) as text) & "\")"
            try
              run script diskLocalRef
            on error e
              error e
            end try
          end try

          

          set the clipboard to diskLocalRef

          

          return

          

        end if
      end repeat
    end if
  end repeat

  

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

  • Prev by Date: Re: duplicate command
  • Next by Date: Re: duplicate command
  • Previous by thread: XML tags as characters in InDesign 5.5
  • Next by thread: Using Home Dir Notation in Do Shell Script
  • Index(es):
    • Date
    • Thread