Friday, December 15, 2017

#1 2017-08-22 02:54:17 pm

pwapub1
Member
Registered: 2017-08-22
Posts: 5

Records - Something weird is happening (read - I do not understand)

Hi all, am a relative newbie to AppleScript, but not programming. I am having trouble understanding why the following script does not work as expected …

Applescript:


-- Script Editor Version is 2.9
-- Applescript appears to be version 2.5
-- OSX Version 10.12.6
-- LNS Script Debugger version is 6.05

use framework "Foundation" -- useful for getting to objC structures
use scripting additions -- Required if using "use" and shell scripts, and other scripting extensions

set theFile to "/Volumes/scratch/images/scripts/working_scripts/test_image2.JPG" as «class furl» -- fixes a bug in Applescript file handling - required when using "use"
set pathList to quoted form of POSIX path of theFile

set recInfo to (info for theFile as alias) as record
set DummyRec to {a:"a", b:"b"}
set testRecord to (recInfo & DummyRec)

on KeysOfRecord(rec)
   set ca to current application
   return (ca's NSDictionary's dictionaryWithDictionary:rec)'s allKeys() as list
end KeysOfRecord


set keyList to KeysOfRecord(testRecord)

keyList

Late Night Software's Script Debugger’s variable-display lists
recInfo as a record with 20 entries
DummyRec as a record with 2 items
testRecord as a record with 22 items

If I use set keyList to KeysOfRecord(recInfo) – keyList is a list with 0 entries - I expected 20
If I use set keyList to KeysOfRecord(DummyRec) – keyList is a list with 2 entries - I expected 2 - works
If I use set keyList to KeysOfRecord(testRecord) – keyList is a list of 2 entries - I expected 22

I would appreciate any help in understanding
  a) Why this script does not work as I expect
  b) How do I fix it (reliably getting file record keys appears to be nearly impossible with AppleScript).

Cheers
Peter

Model: MacBook Pro Late 2011
AppleScript: 2.5
Browser: Safari 603.3.8
Operating System: Mac OS X (10.12.5)

Offline

 

#2 2017-08-22 03:48:15 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4454

Re: Records - Something weird is happening (read - I do not understand)

Hi. Welcome to MacScripter.

The keys in recInfo are the reserved labels from 'info for', which can't be used in NSDictionaries. The keys in DummyRec are your own user-defined labels, which can be used in NSDictionaries and are therefore returned by your script. Basically, there's nothing wrong with the script, just the input. Or to put it another way, you can't get reserved labels that way.


NG

Offline

 

#3 2017-08-22 04:08:25 pm

pwapub1
Member
Registered: 2017-08-22
Posts: 5

Re: Records - Something weird is happening (read - I do not understand)

Hi Nigel,

thanks for the information. Is there a way to do what I am trying to do that you are aware of?
Ultimately I am trying to get this information in a Hazel script so that I can report on a file's information (similar to the get info command).

I have tried several methods but each falls down at a critical point. For example I tried using (see fragment)

Applescript:


try
set TextRecord to theRecord as text
on error theError
set text item delimiters to "{"
set a to text item 2 of theError as string -- gets rid of the Can't make {
set text item delimiters to "}"
set b to text item 1 of a as string -- gets rid of trailing } into type text
set text item delimiters to {"\", ", "\""}
set c to text items of b as list -- creates a list of items
set text item delimiters to tids
end try
c

which works perfectly in the AppleScript editor - but in Hazel reports the types of the keys and not the key names. Why? - I have no idea. I guessed it might be a timing issue.

Any other workable solutions would be truly appreciated. I have spent weeks on this one issue - and have learned a lot in the process - but I am getting very frustrated with this seemingly simple issue.

Cheers,
Peter

Offline

 

#4 2017-08-22 04:58:29 pm

pwapub1
Member
Registered: 2017-08-22
Posts: 5

Re: Records - Something weird is happening (read - I do not understand)

Hi all,

the answer was in another place
http://macscripter.net/viewtopic.php?id=45430
I saw this post some time ago - but did not have the knowledge to use it, until I tried to solve the problem myself. Now I understand it much better.

Thanks for checking my post.

Cheers,
Peter

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)