Jump to content

Help Me Make a Workflow | Display File Size in Large Type


Recommended Posts

Posted

Hi amazing clever Alfred-ites,

 

I would like to make a workflow that would display the size of a selected file in large type. So if I have a file selected in the Finder, I could hit a keystroke and, really big, I would see "1.2 GB" or whatever the file size is.

 

I don't even know where to start. Does anyone have any ideas?

 

Thank you!

 

Liat

Posted

First, create a new workflow (BLANK)

 

From top right hit the + button and select "Triggers" - "Hotkey"

Double click the hotkey trigger, and set your hotkey

Then for Action, select "Pass through to workflow"

Then for Argument, select "Selection in OS X"

Click Save

 

Again click the + at the top right and select "Actions" - "Run Script"

Double click on the Run Script and for language select "osascript (AS)"

Deselect all the Escaping options.

In the script area paste the following:

tell application "Finder"
set selectedItem to (item 1 of (get selection))
set informationList to {}
copy ("Size: " & size of selectedItem & " (" & physical size of selectedItem & ")") to end of informationList
end tell

Then Save

 

Then again, click the top right + and select "Outputs" - "Large Type"

 

Finally connect the Hotkey to the Run Script, and then the Run Script to the Large Type.

 

NOTE: There are many ways of getting file info, the above shows the exact file size and size on disc as shown when simply using CMD + I on a file.

Posted

Thank you so much - you are amazing! It's so cool that you know how to do this! I really appreciate it.

 

I set up the workflow like you said and it works.

 

However, the output is a little confusing if you're not a computer. It displays it like this:

 

Size: 5739242 (5783552)

 

Is there any way to get it to display like it does in the File Info window, like this? ->  Size: 5.7 GB

 

Again, thanks a million. 

 

Posted (edited)

Then for Argument, select "Selection in OS X"

 

You don't need this bit. You get the Finder selection in your AppleScript. 

 

Is there any way to get it to display like it does in the File Info window, like this? ->  Size: 5.7 GB

 

This should do the trick: 

on roundThis(n, places)
	set x to 10 ^ places
	(((n * x) + 0.5) div 1) / x
end roundThis

on humanSize(bytes)
	set i to 1
	set n to bytes
	set units to {"B", "KB", "MB", "GB", "TB"}
	repeat while i < (count of units) and n > 1000
		if n > 1000 then
			set n to n / 1000
			set i to i + 1
		end if
	end repeat
	if i > 2 then
		set n to roundThis(n, 2)
	else
		set n to roundThis(n, 1)
	end if
	return (n as string) & " " & (item i of units) as string
end humanSize

tell application "Finder"
	set selectedItem to (item 1 of (get selection))
	set theBytes to (size of selectedItem)
	"Size: " & my humanSize(theBytes)
end tell

Don't ask me how the roundThis() function works.

Edited by deanishe
Posted

Thanks for chiming in deanishe!

I searched Google for the rounding and found so many it flustered me ... as I'm a bit more comfortable with BASH than OSA.

Posted

 

You don't need this bit. You get the Finder selection in your AppleScript. 

 

 

This should do the trick: 

on roundThis(n, places)
	set x to 10 ^ places
	(((n * x) + 0.5) div 1) / x
end roundThis

on humanSize(bytes)
	set i to 1
	set n to bytes
	set units to {"B", "KB", "MB", "GB", "TB"}
	repeat while i < (count of units) and n > 1000
		if n > 1000 then
			set n to n / 1000
			set i to i + 1
		end if
	end repeat
	if i > 2 then
		set n to roundThis(n, 2)
	else
		set n to roundThis(n, 1)
	end if
	return (n as string) & " " & (item i of units) as string
end humanSize

tell application "Finder"
	set selectedItem to (item 1 of (get selection))
	set theBytes to (size of selectedItem)
	"Size: " & my humanSize(theBytes)
end tell

Don't ask me how the roundThis() function works.

 

 

OH MY GOD YOU ARE AMAZING!!! You have made my life so much easier! Thank you, thank you from the bottom of my heart.

Posted (edited)

OH MY GOD YOU ARE AMAZING!!! You have made my life so much easier! Thank you, thank you from the bottom of my heart.

 

If that's directed at me, I just pasted a few lines of code I found into Rodger's script. Thank him.

 

Left as an exercise to the reader is changing the bytes (and possibly KB) to have zero decimal places and to remove trailing zeroes (and decimal points) from the output.

 

Thanks for chiming in deanishe!

I searched Google for the rounding and found so many it flustered me ... as I'm a bit more comfortable with BASH than OSA.

 

I hate both of them, to be honest. Give me a "real" programming language like Python or Ruby any day. Things you come to take for granted in a programming language are usually super-esoteric in bash and overly-complicated in AppleScript.

 

I think I got the rounding code from Stack Overflow. Like I say, I don't really understand exactly how it works, but I tested it, and it does!

 

EDIT: Tell a lie, the code is from here.

 

EDIT 2: I've had a look at the AppleScript docs and now I do understand how roundThis() works. I'd never have thought of it myself, though…

Edited by deanishe

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...