applescript - jxa - Getting NSAttributedString font metrics in JavaScript for Automation -
in macos sierra javascript automation, can write:
// helvetica12width :: string -> num function helvetica12width(str) { return $.nsattributedstring.alloc.init.initwithstring( str ) .size.width; }
to metrics particular string in default helvetica 12. haven't yet managed pass in attributes other fonts , font sizes, , corresponding metrics those.
has discovered idiom/syntax works here jxa or applescript ?
update: kind of thing have experimented – off mark though, variation in font size/name values doesn't affect return value:
(() => { 'use strict'; objc.import('appkit'); return $.nsattributedstring.alloc.init.initwithstringattributes( "substantiation", { 'nsfontattributename': $.nsfont.fontwithnamesize('helvetica', 24) } ) .size.width })();
ah ... seems it:
(function () { 'use strict'; objc.import('appkit'); // show :: -> string const show = x => json.stringify(x, null, 2); // stringsizeinfontatpointsize :: string -> string -> num // -> {width:num, height:num} function stringsizeinfontatpointsize(str, fontname, points) { return $.nsattributedstring.alloc.init.initwithstringattributes( str, $({ 'nsfont': $.nsfont.fontwithnamesize(fontname, points) }) ) .size; } // test ------------------------------------------------------------------- return show([ stringsizeinfontatpointsize("hello world", "geneva", 32), stringsizeinfontatpointsize("hello world", "geneva", 64), stringsizeinfontatpointsize("hello world", "helvetica", 64), ]); })();
Comments
Post a Comment