coerce constants to text

Hi All,

Is there a way to convert a disk’s properties to text? I need to log disk information

tell application "Finder"
   set volInfo to  properties of disk diskName
end tell
log volInfo

That returns


Can't make «class pcmp» of application "Finder" into the expected type.

Thanks, Rob

Hello.

local tids
set tids to AppleScript's text item delimiters
tell application "Finder"
	set volInfo to properties of disk (path to startup disk)
	set the clipboard to volInfo
	set c to the clipboard
	try
		log c as text
	on error e
		set AppleScript's text item delimiters to {"Can't make", "into type text."}
		set d to text item 2 of e
		log d
		set AppleScript's text item delimiters to tids
	end try
end tell
set AppleScript's text item delimiters to tids

Hi.

That’s because the properties record contains labels only the Finder understands and you’ve got the ‘log’ command outside the Finder ‘tell’ statement.

If you want to save the log, you can get the record as text by executing the Finder command with osascript in a shell script:

set volInfo to (do shell script ("osascript -e 'tell app \"Finder\" to get properties of disk \"" & diskName & "\"' -s s"))

Here is a script doing the job for every version.


set diskName to "archives AppleScript_Users"
tell application "Finder"
	set volInfo to properties of disk diskName
	try
		volInfo as text
	on error errMsg
		set off7 to offset of "{" in errMsg
		set off_7 to offset of "}" in (reverse of text items of errMsg as text)
		set errMsg to text off7 thru -off_7 of errMsg
	end try
end tell
errMsg

The beginning and the end of the error message are localized.
In French there are :

Il est impossible de rendre {class:.:true} en type text.
So I drop characters according to the location of the firt “{” and the last “}”.

KOENIG Yvan (VALLAURIS, France) mardi 30 avril 2013 11:13:42

Hi Yvan.

Surprisingly, your method renders Finder keywords as chevron codes on my system, even when run in AppleScript Editor.

And it assumes that AppleScript’s text item delimiters will be {“”} or “” at the time.

It’s not the script which replace keywords by chevron codes, it’s AppleScript which issue the error message this way.

For the delimiters, it would be better to save the original one and reset it on exit but I grabbed the code from an application in which the current item is the empty string.

KOENIG Yvan (VALLAURIS, France) mardi 30 avril 2013 12:17:05

:slight_smile:

tell application "Finder"
	set volInfo to properties of disk (path to startup disk)
	log volInfo
end tell

Thanks Nigel,

That’s just what I was looking for. Simple and clean.

Rob

Hello McUsrII

What need to log the properties ?
The log window already display the result of the instruction
set volInfo to properties of disk (path to startup disk)


tell application "Finder"
	path to startup disk
		--> alias "Macintosh HD:"
	get properties of disk (alias "Macintosh HD:")
		--> {class:disk, name:"Macintosh HD", index:2, displayed name:"Macintosh HD", name extension:"", extension hidden:false, container:computer container, disk:startup disk, position:{-1, -1}, desktop position:{1871, 50}, bounds:{-33, -33, 31, 31}, kind:"Volume", label index:0, locked:false, description:missing value, comment:"", size:5.23262251008E+11, physical size:5.23262251008E+11, creation date:date "dimanche 19 août 2012 20:07:17", modification date:date "vendredi 12 avril 2013 19:07:41", icon:missing value, URL:"file://localhost/", owner:"système", group:"root", owner privileges:read write, group privileges:read only, everyones privileges:read only, container window:container window of startup disk, id:-100, capacity:9.99345127424E+11, free space:4.76082876416E+11, ejectable:false, startup:true, format:Mac OS Extended format, journaling enabled:true, local volume:true, ignore privileges:false}
	(*class:disk, name:Macintosh HD, index:2, displayed name:Macintosh HD, name extension:, extension hidden:false, container:computer container, disk:startup disk, position:-1, -1, desktop position:1871, 50, bounds:-33, -33, 31, 31, kind:Volume, label index:0, locked:false, description:missing value, comment:, size:5.23262251008E+11, physical size:5.23262251008E+11, creation date:date dimanche 19 août 2012 20:07:17, modification date:date vendredi 12 avril 2013 19:07:41, icon:missing value, URL:file://localhost/, owner:système, group:root, owner privileges:read write, group privileges:read only, everyones privileges:read only, container window:container window of startup disk, id:-100, capacity:9.99345127424E+11, free space:4.76082876416E+11, ejectable:false, startup:true, format:Mac OS Extended format, journaling enabled:true, local volume:true, ignore privileges:false*)
end tell

I see no significant difference between the result and the logged value.

KOENIG Yvan (VALLAURIS, France) mardi 30 avril 2013 13:39:39

The OP wanted to obtain the log results automatically, without watching the log window.

Or maybe I’m missing the point about human readable values from the OSA script? :slight_smile:

I wasn’t.

The osascript code put the infos in a text item which may be deciphered or stored by a script continuing to run.

What may you do with the log without stopping the script ?
I never read a piece of code able to decipher or store the content of the log.
The only tip which I am aware of is to copy it by hand from the log window.


path to startup disk
tell application "System Events" to set diskName to name of result

set volInfo to (do shell script ("osascript -e 'tell app \"Finder\" to get properties of disk \"" & diskName & "\"' -s s"))

my writeto((path to desktop as text) & "human readable.txt", volInfo, text, false)

# the script may continue here


#=====
(*
Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861
*)
on writeto(targetFile, theData, dataType, apendData)
	-- targetFile is the path to the file you want to write
	-- theData is the data you want in the file.
	-- dataType is the data type of theData and it can be text, list, record etc.
	-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
	try
		set targetFile to targetFile as text
		set openFile to open for access file targetFile with write permission
		if not apendData then set eof of openFile to 0
		write theData to openFile starting at eof as dataType
		close access openFile
		return true
	on error
		try
			close access file targetFile
		end try
		return false
	end try
end writeto

#=====

KOENIG Yvan (VALLAURIS, France) mardi 30 avril 2013 17:29:14

Hello.

If you write the log to a log file, then there are preferences in Console.app, that brings the log window to front.

I believe that you can configure this for any open log, that is, that the log is displayed in a window of Console.app.