Forked from zarigani/new_rtf_file_with_margin.scpt
Last active
April 22, 2016 23:00
-
-
Save dmland/ec89a7608c425575df2da9433ff74f9b to your computer and use it in GitHub Desktop.
TextEdit margin setting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Based on https://gist.github.com/zarigani/400795 | |
-- This version is in English with English (inch) units | |
-- Original was in Japanese with Metric units | |
set margin to input_margin() | |
tell application "TextEdit" | |
activate | |
make new document | |
set text of front document to my rtf_setting(margin) | |
set tmp_path to (path to temporary items as text) & "rtf_margin_setting.scpt.rtf" | |
set doc_name to "Margin " & my in_text(margin) & ".rtf" | |
close front document saving yes saving in tmp_path | |
open file (tmp_path) | |
set name of front document to doc_name | |
end tell | |
on input_margin() | |
set margin to false | |
repeat until margin | |
set my text item delimiters to {",", " "} | |
"Page margins(T,B,L,R) in inches: | |
" & " | |
" & "Examples: | |
" & " 0.5 → T,B,L,R=0.5in; | |
" & " 1,0.5 → T,B=1in; L,R=0.5in; | |
" & " 1,0.75,0.5,0.5 → T=1in, B=0.5in, L,R=0.75in; | |
" & " | |
" & "Add '+ ' before T,B to ensure room for header & footer: '+ 5,+ 20,10,5,0'" | |
display dialog result default answer "" buttons {"Cancel", "OK"} default button 2 with icon note | |
set margin to result's text returned's text items | |
set my text item delimiters to "" | |
if margin's number = 1 then | |
twip(margin's item 1, margin's item 1, margin's item 1, margin's item 1) & {margin} | |
else if margin's number = 2 then | |
twip(margin's item 1, margin's item 1, margin's item 2, margin's item 2) & {margin} | |
else if margin's number = 4 then | |
twip(margin's item 1, margin's item 2, margin's item 3, margin's item 4) & {margin} | |
else | |
beep | |
set margin to false | |
end if | |
end repeat | |
end input_margin | |
on rtf_setting(margin) | |
set rtf1 to "{\\rtf1\\mac\\ansicpg10001\\cocoartf824\\cocoasubrtf470" & return | |
set end_rtf1 to "}" | |
set paperw to {"\\paperw", 10800} --7.5in--US Letter | |
set paperh to {"\\paperh", 15840} --11in--US Letter | |
set margl to {"\\margl", margin's item 3} | |
set margr to {"\\margr", margin's item 4} | |
set margt to {"\\margt", margin's item 1} | |
set margb to {"\\margb", margin's item 2} | |
set vieww to {"\\vieww", 999999} | |
set viewh to {"\\viewh", 999999} | |
set viewkind to {"\\viewkind", 1} | |
set viewscale to {"\\viewscale", 100} | |
set page_setting to paperw & paperh & margl & margr & margt & margb & vieww & viewh & viewkind & viewscale & return as text | |
rtf1 & page_setting & end_rtf1 | |
end rtf_setting | |
on in_text(margin) | |
join {margin's item 5, "_"} | |
end in_text | |
on twip(t, b, l, r) | |
set twip_margin to {twip_form(t), twip_form(b), twip_form(l), twip_form(r)} | |
if t's item 1 is "+" and twip_margin's item 1 < 576 then set twip_margin's item 1 to 576 | |
if b's item 1 is "+" and twip_margin's item 2 < 288 then set twip_margin's item 2 to 288 | |
twip_margin | |
end twip | |
on twip_form(inch) | |
round (inch as number) * 1440 | |
end twip_form | |
on join {sourceList, delimiter} | |
set oldDelimiters to my text item delimiters | |
set AppleScript's text item delimiters to {delimiter} | |
set theText to sourceList as text | |
set my text item delimiters to oldDelimiters | |
return theText | |
end join |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment