Script: Display file size

This script displays the frontmost Tinderbox’s file size.

Example use case

I often drag images into Tinderbox and then end up with hugh files because I tend to forget to adjust the images before dragging. The capture shows the file size of a Tinderbox with only 3 images.

And this capture shows the file size after adjusting the images in Retrobatch.

Setup

  • The Tinderbox needs to be saved on disk
    If you open a new Tinderbox and try the script without saving the Tinderbox it won’t work.

  • To test the script via Script Debugger or Script Editor you can set property testScriptto true.
    When you run the script this will hide the script window in order to make Tinderbox frontmost.

  • The file size alert is automatically dismissed after 2.5 seconds
    If you want to change that value see the line that contains “giving up after”.

  • Run the script via e.g. Alfred or Keyboard Maestro.

Script

-- Display file size

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

property testScript : false

if testScript = true then
	set hideScriptWindow to my front_testing()
end if

---------------------------------------------------- Get file URL of frontmost Tinderbox -----------------------------------------------------

tell application "System Events"
	try
		set frontProcess to name of first process whose frontmost is true
		
		if frontProcess ≠ "Tinderbox 10" then
			display alert "System Events" message "Tinderbox is not frontmost" as warning
			return
		end if
		
		tell application process "Tinderbox"
			tell window 1
				set theFile_URL to the value of attribute "AXDocument"
				set theFile_Name to the value of attribute "AXTitle"
			end tell
		end tell
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "System Events" message error_message as warning
		return
	end try
end tell

---------------------------------------------------------------- Get file path -----------------------------------------------------------------

set theFile_Path to (current application's NSString's stringWithString:(text item 2 of (my tid(theFile_URL, "//"))))'s stringByRemovingPercentEncoding()

----------------------------------------------------------------- Get file size ------------------------------------------------------------------

set theFile_Attributes to current application's NSFileManager's defaultManager's attributesOfItemAtPath:(theFile_Path) |error|:(missing value)
set theFile_Size_inBytes to (theFile_Attributes's objectForKey:(current application's NSFileSize))

------------------------------------------------------------ Get file size as string -------------------------------------------------------------

set theFile_Size_string to (current application's NSByteCountFormatter's stringFromByteCount:theFile_Size_inBytes countStyle:(current application's NSByteCountFormatterCountStyleDecimal)) as string

--------------------------------------------------------------- Display file size ----------------------------------------------------------------

tell application id "Cere"
	try
		activate
		display alert "File size" buttons {"Ok"} default button 1 message " " & linefeed & theFile_Size_string & linefeed & "" as informational giving up after 2.5
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "Tinderbox" message error_message as warning
		return
	end try
end tell

-------------------------------------------------------------- Handler Section ---------------------------------------------------------------

on tid(theInput, theDelimiter)
	set d to AppleScript's text item delimiters
	set AppleScript's text item delimiters to theDelimiter
	if class of theInput = text then
		set theOutput to text items of theInput
	else if class of theInput = list then
		set theOutput to theInput as text
	end if
	set AppleScript's text item delimiters to d
	return theOutput
end tid

on front_testing()
	tell application "System Events"
		set frontmostProcess to first process whose frontmost is true
		set visible of frontmostProcess to false
		repeat while (frontmostProcess is frontmost)
			delay 0.2
		end repeat
		set secondFrontmost to first process whose frontmost is true
	end tell
end front_testing

2 Likes

Cool.

Pasting media in a Tinderbox file is a fine practice for a limited set of use cases, I’ve found. Your solution is a great contribution to this practice.

Personally, I’ve found keeping the media on disk and pulling it via templates and manipulating its display with action code and CSS has much one logovitiy and produces greater efficiencies and effectiveness for media (images, audio, tables, video) use. For those interested, you can check out my method here:

Michael
5Cs School (next 5Cs Mastering Tinderbox 6-Week Cohort kicks off April 11th).