Convert InDesign Applescript to javascript
Hi Everyone,
At a previous job of mine, a colleague had developed and shared an awesome script that cleans all the whitespace out of InDesign layouts (Multiple Space to Single Space, Remove Leading Whitespace, Remove Trailing Whitespace, Multiple Return to Single Return). This thing works like magic.
Every job I've had since getting this script were in Mac environments. The only thing I had to do to make it work on later versions of InDesign was to edit "tell application" to the updated app. However, right now I'm working remotely and I've been forced to use my PC to get work done and it would be really helpful if I had a javascript version of it.
Of course you guys can use this and/or pass it around so long as you credit my colleague. His info is in the prologue of the script.
Warning: this script does not play well with designers who do not lay their text out properly. If you use double returns and multiple spaces or tabs in your layout instead of proper paragraph styles with "space after", this script will wreck the layout.
-----------------------------------------------------------------------------------
--
-- Script Name: Clean Text White Space.scpt
-- Version: 1.0
-- Author: Brendan McBryan
-- Contact: brendan.mcbryan@gmail.com
-- Date: March 3, 2011
-- Application: Adobe InDesign CS4
--
-- Description: This AppleScript is designed to be run from the scripts panel in InDesign CS4. It will run the most common Find & Change actions relating to cleaning up white space in Text.
--
--The Actions included are: Multiple Space to Single Space, Remove Leading Whitespace, Remove Trailing Whitespace, Multiple Return to Single Return.
--
-----------------------------------------------------------------------------------
tell application "Adobe InDesign CC 2018"
--MULTIPLE SPACE TO SINGLE SPACE
--Clear the find/change preferences.
set find grep preferences to nothing
set change grep preferences to nothing
--Load Find/Change variables for Multiple Space to Single Space
set varFind to "[~m~>" & "~f~|~S~s~<~/~.~3~4~% ]{2,}" as string
set varChange to "\\s" as string
set include hidden layers of find change grep options to true
set the find what of find grep preferences to varFind
set the change to of change grep preferences to varChange
-- Execute Multiple Space to Single Space
tell active document
set varFoundMultipleSpace to change grep
end tell
--REMOVE LEADEING WHITESPACE
--Clear the find/change preferences.
set find grep preferences to nothing
set change grep preferences to nothing
--Load Find/Change variables for Remove Leading Whitespace
set varFind to "^ " as string
set varChange to "" as string
set include hidden layers of find change grep options to true
set the find what of find grep preferences to varFind
set the change to of change grep preferences to varChange
-- Execute Multiple Space to Remove Leading Whitespace
tell active document
set varFoundLeadingSpace to change grep
end tell
--MULTIPLE RETURN TO SINGLE RETURN
--Clear the find/change preferences.
set find grep preferences to nothing
set change grep preferences to nothing
--Load Find/Change variables for Multiple Return to Single Return
set varFind to "~b~b+" as string
set varChange to "\\r" as string
set include hidden layers of find change grep options to true
set the find what of find grep preferences to varFind
set the change to of change grep preferences to varChange
-- Execute Multiple Return to Single Return
tell active document
set varFoundMultipleReturn to change grep
end tell
--REMOVE TRAILING WHITESPACE
--Clear the find/change preferences.
set find grep preferences to nothing
set change grep preferences to nothing
--Load Find/Change variables for Remove Trailing Whitespace
set varFind to "\\s+$" as string
set varChange to "" as string
set include hidden layers of find change grep options to true
set the find what of find grep preferences to varFind
set the change to of change grep preferences to varChange
-- Execute Remove Trailing Whitespace
tell active document
set varFoundTrailingSpace to change grep
end tell
--Display Change Count
set varAnswer to the button returned of (display dialog ¬
("Changed " & (count varFoundMultipleSpace) & " Multiple Spaces to Single Spaces" & return & ¬
"Removed " & (count varFoundLeadingSpace) & " instances of Leading Space" & return & ¬
"Changed " & (count varFoundMultipleReturn) & " Multiple Returns to Single Returns" & return & ¬
"Removed " & (count varFoundTrailingSpace) & " instances of Trailing Space") ¬
as string ¬
buttons {"Undo", "Thanks!"} ¬
with title "Success!")
--Undo if requested
if varAnswer = "Undo" then
tell application "System Events"
repeat 4 times
keystroke "z" using command down
end repeat
end tell
end if
end tellThis script does a grep find and change for several things, prompts you with how many things it changed, and gives you the option to undo what it did.
Please let me know if anyone can help with this. Thanks!!
