• 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: File-Info as Text
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: File-Info as Text


  • Subject: Re: File-Info as Text
  • From: "koenig.yvan" <email@hidden>
  • Date: Mon, 18 Aug 2014 10:05:58 +0200


Le 18/08/2014 à 07:25, Shane Stanley <email@hidden> a écrit :

On 17 Aug 2014, at 6:33 am, Christopher Stone <email@hidden> wrote:

For a while now I've been meaning to write a file-info script to grab the info I want as text and make it easy to grab any of the bits I want.

I've had keyboard-driven scripts to put the alias, or relative alias, or posix path(s) of of the Finder selection on the clipboard for well over a decade, but sometimes it's nice to have the whole enchilada to choose from.

This got me thinking about the whole business of file information, and the 'info for' command that is at the heart of your script.

…
Anyway, here's a script library that returns quite a bit more info. It returns it either as a text version of a property list, or if you have ASObjCExtras.framework installed, as a record. All it needs is someone like Chris to format the result nicely...

use framework "Foundation"
-- use framework "ASObjCExtras" ###### If you have ASObjCExtras.framework installed, uncomment this line


on moreInfoFor:POSIXPath dirSizes:sizeBoolean
-- get what we can from URL
set anNSURL to current application's NSURL's fileURLWithPath:POSIXPath
-- build list of keys for values we want
set valuesList to {current application's NSURLCreationDateKey, current application's NSURLContentModificationDateKey, current application's NSURLContentAccessDateKey, current application's NSURLNameKey, current application's NSURLFileSizeKey, current application's NSURLIsDirectoryKey, current application's NSURLIsAliasFileKey, current application's NSURLIsPackageKey, current application's NSURLIsHiddenKey, current application's NSURLHasHiddenExtensionKey, current application's NSURLLocalizedNameKey, current application's NSURLLocalizedTypeDescriptionKey, current application's NSURLTypeIdentifierKey, current application's NSURLIsWritableKey, current application's NSURLIsExcludedFromBackupKey, current application's NSURLIsExecutableKey, current application's NSURLIsSymbolicLinkKey, current application's NSURLLabelNumberKey, current application's NSURLLinkCountKey, current application's NSURLTagNamesKey, current application's NSURLFileAllocatedSizeKey, current application's NSURLTotalFileSizeKey, current application's NSURLTotalFileAllocatedSizeKey}
-- fetch values for our keys
set {attsNSDictionary, theError} to anNSURL's resourceValuesForKeys:valuesList |error|:(reference)
if attsNSDictionary is missing value then -- something went wrong, throw error
error (theError's localizedDescription() as text)
end if
-- make a mutable copy so we can modify values
set attsNSMutableDictionary to attsNSDictionary's mutableCopy()
-- handle tags: if there are none there will be no value, so we will put in an empty list
set theTags to (attsNSDictionary's valueForKey:(current application's NSURLTagNamesKey))
if theTags is missing value then
attsNSMutableDictionary's setObject:{} forKey:(current application's NSURLTagNamesKey)
end if
-- do stuff just for direcetories
if (attsNSDictionary's valueForKey:(current application's NSURLIsDirectoryKey)) then
set theNSBundle to current application's NSBundle's bundleWithURL:anNSURL
set bundleInfoNSDictionary to theNSBundle's infoDictionary()
attsNSMutableDictionary's addEntriesFromDictionary:bundleInfoNSDictionary
if sizeBoolean as boolean then
-- we need to get the total size of all items in the directory
set {s1, s2, s3, s4} to my fileSizeFor:anNSURL rawSize:0 allocatedSize:0 totalSize:0 totalAllocatedSize:0
attsNSMutableDictionary's setValue:s1 forKey:(current application's NSURLFileSizeKey)
attsNSMutableDictionary's setValue:s2 forKey:(current application's NSURLFileAllocatedSizeKey)
attsNSMutableDictionary's setValue:s3 forKey:(current application's NSURLTotalFileSizeKey)
attsNSMutableDictionary's setValue:s4 forKey:(current application's NSURLTotalFileAllocatedSizeKey)
end if
end if
-- get the Spotlight metadata
set theNSMetadataItem to current application's NSMetadataItem's alloc()'s initWithURL:anNSURL
set theAtts to theNSMetadataItem's attributes()
set theMetadata to theNSMetadataItem's valuesForAttributes:theAtts
attsNSMutableDictionary's addEntriesFromDictionary:{SpotlightData:theMetadata}
-- get some extended attributes. This is undocumented API so may be dodgy, but it seems OK for these two values...
set theNSFileManager to current application's NSFileManager's defaultManager()
set allAtts to theNSFileManager's attributesOfItemAtPath:POSIXPath |error|:(missing value)
set extendedAtts to allAtts's valueForKey:"NSFileExtendedAttributes"
if extendedAtts is not missing value then
set isQuarantined to extendedAtts's objectForKey:"com.apple.quarantine"
if isQuarantined is missing value then
attsNSMutableDictionary's setValue:false forKey:"isQuarantined"
else
attsNSMutableDictionary's setValue:true forKey:"isQuarantined"
end if
set textEncoding to extendedAtts's objectForKey:"com.apple.TextEncoding"
if textEncoding is not missing value then
set textEncoding to current application's NSString's alloc()'s initWithData:textEncoding encoding:(current application's NSUTF8StringEncoding)
attsNSMutableDictionary's setValue:textEncoding forKey:"textEncoding"
end if
end if
-- get the app that would open it
set theNSWorkspace to current application's NSWorkspace's sharedWorkspace()
set appURL to theNSWorkspace's URLForApplicationToOpenURL:anNSURL
attsNSMutableDictionary's setValue:appURL forKey:"defaultApplication"
###### If you have ASObjCExtras.framework installed, uncomment the following line
-- return (current application's SMSFord's fordIn:attsNSMutableDictionary) as record
return attsNSMutableDictionary's |description|() as text

end moreInfoFor:dirSizes:

on fileSizeFor:aURL rawSize:theSize allocatedSize:allocSize totalSize:totSize totalAllocatedSize:totalAllocSize
set theNSFileManager to current application's NSFileManager's defaultManager()
set valuesList to {current application's NSURLFileSizeKey, current application's NSURLFileAllocatedSizeKey, current application's NSURLTotalFileSizeKey, current application's NSURLTotalFileAllocatedSizeKey, current application's NSURLIsDirectoryKey}
set theFiles to (theNSFileManager's contentsOfDirectoryAtURL:aURL includingPropertiesForKeys:valuesList options:0 |error|:(missing value)) as list
repeat with anNSURL in theFiles
set attsNSDictionary to (anNSURL's resourceValuesForKeys:valuesList |error|:(missing value))
if (attsNSDictionary's valueForKey:(current application's NSURLIsDirectoryKey)) as boolean then
set {theSize, allocSize, totSize, totalAllocSize} to (my fileSizeFor:anNSURL rawSize:theSize allocatedSize:allocSize totalSize:totSize totalAllocatedSize:totalAllocSize)
else
set theSize to (theSize as integer) + ((attsNSDictionary's valueForKey:(current application's NSURLFileSizeKey)) as integer)
set allocSize to (allocSize as integer) + ((attsNSDictionary's valueForKey:(current application's NSURLFileAllocatedSizeKey)) as integer)
set totSize to (totSize as integer) + ((attsNSDictionary's valueForKey:(current application's NSURLTotalFileSizeKey)) as integer)
set totalAllocSize to (totalAllocSize as integer) + ((attsNSDictionary's valueForKey:(current application's NSURLTotalFileAllocatedSizeKey)) as integer)
end if
end repeat
return {theSize, allocSize, totSize, totalAllocSize}
end fileSizeFor:rawSize:allocatedSize:totalSize:totalAllocatedSize:

Run it with:

set thePath to POSIX path of (choose file)
tell script ",script name>"
set theResult to its moreInfoFor:thePath dirSizes:false
end tell


-- 
Shane Stanley <email@hidden>

It seems that I'm really an ass when I try to use a library.

As I fully agree with what Shane wrote, I saved the code posted above as : SSD 500:Library:Script Libraries:moreInfoFor Lib.scptd:
I correctly checked the box entitled :  AppleScript/Bibliothèque Objective-C (Yes, I’m running the system in French)
I checked that Foundation.frameworks is available :
SSD 500:System:Library:Frameworks:Foundation.framework:
I checked also that ASObjCExtras.framework is available :
SSD 500:Library:Frameworks:ASObjCExtras.framework:

I ran the caller script :

set thePath to POSIX path of (choose file)
tell script "moreInfoFor Lib"
set theResult to its moreInfoFor:thePath dirSizes:false
end tell

and I got :
error "Il est impossible d’obtenir framework \"Foundation\" of «script \"moreInfoFor Lib\"». Accès non autorisé." number -1723 from framework "Foundation"

Really frustrating because I ran several other scripts using libraries using Foundation which behave well. An example is findDatesIn Lib.scptd posted by Shane some weeks ago.
Just for see, I tried to run a modified caller :

set theLib to script "moreInfoFor Lib"
set thePath to POSIX path of (choose file)
set theResult to theLib's moreInfoFor:thePath dirSizes:false


And got the same error message.

What am’I doing wrongly ?

Yvan KOENIG (VALLAURIS, France) lundi 18 août 2014 10:05:54

 _______________________________________________
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: File-Info as Text
      • From: Shane Stanley <email@hidden>
References: 
 >File-Info as Text (From: Christopher Stone <email@hidden>)
 >Re: File-Info as Text (From: Shane Stanley <email@hidden>)

  • Prev by Date: Re: File-Info as Text
  • Next by Date: Re: File-Info as Text
  • Previous by thread: Re: File-Info as Text
  • Next by thread: Re: File-Info as Text
  • Index(es):
    • Date
    • Thread