• 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: Days and hours
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Days and hours


  • Subject: Re: Days and hours
  • From: "Mark J. Reed" <email@hidden>
  • Date: Thu, 20 Oct 2005 00:29:32 -0400

On 10/19/05, Gary (Lists) <email@hidden> wrote:
I've been surprised at how fast calling PHP via 'do shell' is compared to
osax calls.  My own tests have led me to use PHP for tasks like 'basename()'
and 'filename()' because it's just faster than AS-only versions of the same.

I'm sure there are tricks to speed up AS's folding, spindling, and mutilating of strings, but once you've gone to all the trouble of firing up the shell, it seems a shame to use it to do nothing but turn around and fire up PHP, especially for something as simple as basename/dirname (which is what I assume you meant, since I don't know of any PHP function called 'filename').

First of all, once you have a file, you can just get its containing folder directly from the Finder:

set someFile to (choose file)
tell application "Finder"
    set dirname to folder of someFile
    set basename to name of someFile
end tell

which, since Finder is always running, should be faster even than the shell alternatives. The resulting value in "dirname" is a Finder folder object, not a pathname, but that's easily rectified if you need the latter:

    set dirname to POSIX path of (folder of someFile as alias)

Using basename with the optional second argument to strip suffixes, on the other hand, is rather more verbose in native AS.

Other solutions:

1. Instead of running the PHP interpreter, just run the special commands basename or dirname, which have a much smaller footprint and run faster:

set someFile to (choose file)
set fullPath to POSIX path of someFile
set basename to do shell script "basename " & quoted form of fullPath
set dirname to do shell script "dirname " & quoted form of fullPath

the basename command has the same optional second argument feature:

set basename to do shell script "basename " & quoted form of fullPath & " .php"

2. Instead of running some other program from the shell, let the shell do it itself with its built-in string magic.

set someFile to (choose file)
set fullPath to POSIX path of someFile
set basename to do shell script "f=" & quoted form of fullPath & "; echo \"${f##*/}\""
set dirname to do shell script "f=" & quoted form of fullPath & "; echo \"${f%/*}\""

To get the suffix removal:

set basename to do shell script "f=" & quoted form of fullPath & "; f=\"${f##*/}\"; echo \"${f%.php]\""

This works regardless of the specific suffix:

set basename to do shell script "f=" & quoted form of fullPath & "; f=\"${f##*/}\"; echo \"${f%.*]\""

--
Mark J. Reed <email@hidden>
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Days and hours
      • From: "Mark J. Reed" <email@hidden>
    • Re: Days and hours
      • From: Shane Stanley <email@hidden>
References: 
 >Re: Days and hours (From: Christopher Nebel <email@hidden>)
 >Re: Days and hours (From: "Gary (Lists)" <email@hidden>)

  • Prev by Date: Re: Slow script with long lists
  • Next by Date: Re: Days and hours
  • Previous by thread: Re: Days and hours
  • Next by thread: Re: Days and hours
  • Index(es):
    • Date
    • Thread