INDESIGN: Getting Find What / Change To Fields of Not-run Search
INDESIGN: Getting Find What / Change To Fields of Not-run Search
- Subject: INDESIGN: Getting Find What / Change To Fields of Not-run Search
- From: Rick Gordon <email@hidden>
- Date: Thu, 11 Jul 2013 15:19:40 -0700
Title: INDESIGN: Getting Find What / Change To Fields of
Not-run
I'm setting up a script to enable search/replace (text or GREP)
to function when target elements are locked. (By design, you can only
find elements in locked frames but not replace them.)
I've got the script pretty much working, but I notice that the
find what and change to attributes of find text/grep preferences and
change text/grep preferences don't appear to be set until a find has
been done.
So there are times when the user might have edited the contents
of those fields, but not actually run a find on them, since the point
would be to use the script.
So how might I be able to get the contents of those fields as
they are currently populated in the dialog, regardless of whether it's
been run?
For reference, the script is included below:
(* Allows Find/Replace in locked
text frames using the existing Text or GREP dialog settings *)
(* Copyright ©2013 by Rick Gordon *)
(* THESE VALUES CAN BE CHANGED BY THE USER'S CHOICES, WHICH WILL BE
REFLECTED IN FUTURE RUNS OF THE SCRIPT. *)
property
pModeResult :
{"Text"}
property pScopeResult :
{"Document"}
(* SET UP SOME NON-PRESISTENT
VARIABLES TO BASIC DEFAULTS. *)
set
vNoSelection to true
set
vIsTextSelection to false
set
vScopeList
to {"Document"}
set vResultList to
{}
(* LET USER CHOOSE FIND VS.
GREP MODE FROM LIST, RECORD CHOICE, AND SET IT AS FUTURE DEFAULT.
*)
set
pModeResult
to choose
from list {"Text",
"GREP"} default
items {pModeResult}
tell
application "Adobe
InDesign CS5.5"
(* ABORT IF NO DOCS ARE
OPEN. *)
set
vDocumentCount to count documents
if
vDocumentCount = 0 then
tell me
to
display alert "No
documents are open."
return
end if
(* SET UP FIND VS. GREP
MODE, BASED ON USER CHOICE;
SET OPTION TO INCLUDE LOCKED LAYERS IN SEARCH.
ABORT IF FIELDS ARE INVALID. *)
if
pModeResult
is {"Text"} then
set
vFindChangeOptions to a reference to find change text options
set
vFindWhat
to find what of find text preferences
set
vChangeTo
to change to of change text preferences
else
set
vFindChangeOptions to a reference to find change grep options
set
vFindWhat
to find what of find grep preferences
set
vChangeTo
to change to of change grep preferences
end if
if (vFindWhat is
"") or (vChangeTo is
"") then
tell me
to
display alert "The
\"Find what\" and \"Change to\" fields cannot be
empty." &
return & return & "FIND WHAT:" & return &
vFindWhat & return &
return & "CHANGE TO:"
& return & vChangeTo
return {some item of
pModeResult, vFindWhat,
vChangeTo}
end if
tell vFindChangeOptions
set
vOriginalLockedLayersSetting to
include locked layers for find
set
include locked layers for find to
true
end tell
(* DETERMINE IF
THERE IS A SELECTION AND, IF SO, IF IT'S TEXT. *)
if
selection
is not {} then
set
vNoSelection to false
if (class of
selection
is
text)
or (class of
selection
is
insertion point) or (class of
selection
is
line)
then
set
vIsTextSelection to true
end if
end if
(* IF MORE THAN ONE DOC
IS OPEN, ADD ALL DOCUMENTS CHOICE. *)
if
vDocumentCount > 1 then
set end
of
vScopeList
to "All
Documents"
end if
(* IF THERE'S A
SELECTION, CHECK FOR MULTIPLE PARENT STORIES; SET APPROPRIATE CHOICE.
*)
if
vNoSelection is
false
then --AT LEAST ONE STORY MAY
BE A TARGET.
try
get item 2 of (parent story of
selection) --TRY TO GET A SECOND STORY.
set end of vScopeList to
"Stories" --ON
SUCCESS, USE "STORIES".
on error
set
end of
vScopeList
to
"Story" --ON FAILURE,
USE "STORY".
end try
(* AND IF
THERE'S A TEXT SELECTION OR INSERTION POINT, CHECK IF IT GOES TO END
OF STORY;
IF NOT, GIVE "TO END OF STORY"
CHOICE. *)
if (vIsTextSelection is
true)
and (index of last insertion point of
selection
is not equal to index of last insertion point of
parent story of
selection) then
set
end of
vScopeList
to "To End of
Story"
end if
(* AND IF
THERE'S A TEXT SELECTION, AND IT'S NOT EMPTY, ADD SELECTION AS A
CHOICE. *)
if (vIsTextSelection is
true)
and (class of
selection
is not
insertion point) then
set
end of
vScopeList
to "Selection"
end if
end if
(* LET USER CHOOSE SCOPE
FROM LIST RECORD CHOICE, AND SET IT AS FUTURE DEFAULT. *)
tell me to set pScopeResult to choose from list vScopeList
default items {pScopeResult}
(* SET UP TARGET(S) FOR
SEARCH BASED ON CHOSEN SCOPE. *)
if
pScopeResult is
{"Document"} then
--set
vTargetList to stories of document 1
set
vTargetList
to
document 1
else if pScopeResult is
{"All Documents"} then
set
vTargetList
to every
document
else if (pScopeResult is
{"Story"}) or
(pScopeResult is {"Stories"}) then
set
vTargetList
to parent story of selection
else if (pScopeResult is
{"To End of Story"}) then
set
vStartIndex
to
index of
first
insertion point of
selection
set
vEndIndex
to
index of
last
insertion point of parent
story
of selection
tell parent story of
selection
set
vTargetList
to object
reference
of
text
from (insertion
point vStartIndex) to
(insertion point
vEndIndex)
end tell
else if pScopeResult is {"Selection"} then
set
vTargetList
to object
reference
of selection
end if
(* SET UP TARGET(S) FOR
SEARCH BASED ON CHOSEN SCOPE;
IF ONLY ONE ITEM, ENCLOSE IN A LIST FOR CONSISTENT
PROCESSING. *)
if class of
vTargetList
is not
list
then
set
vTargetList
to {vTargetList}
set vItemCount to
1
else
set
vItemCount
to length of vTargetList
end if
(* ITERATE THRU LIST;
THERE MAY ONLY BE ONE ITEM. GO IN REVERSE TO AVOID INVALIDATING
REFERENCES. *)
repeat with i from
vItemCount
to 1
by -1
tell object reference of item i of
vTargetList
(* CHOOSE FIND VS. GREP MODE, BASED ON PRIOR USER CHOICE.
JUST FIND THEM, AND GET THE
TARGET FRAMES TO MAY NEED TO BE UNLOCKED. *)
set
vFoundList
to {}
try
if
pModeResult
is {"Text"} then
set
vFoundList
to find
text
else
set
vFoundList
to find
grep
end if
end
try
if
vFoundList
is not {} then
(* UNLOCK LOCKED TARGET PARENT FRAMES &
RECORD THEIR STATUS FOR POST-PROCESSING. *)
repeat with vItem in
vFoundList
set vWasLockedStatusList to
{}
set
vParentFrameList to parent text
frames
of vItem
repeat with vFrame in
vParentFrameList
tell vFrame
set vWasLockedStatus to locked
set end
of
vWasLockedStatusList to
vWasLockedStatus
if vWasLockedStatus is true then set locked to false
end tell
end repeat
(* CHOOSE FIND VS. GREP MODE, BASED ON
PRIOR USER CHOICE,
AND DO THE
REPLACEMENTS IN REVERSE ORDER TO AVOID INVALIDATING
REFERENCES. . *)
if
pModeResult
is {"Text"} then
set vFoundList to change text with
reverse order
else
set vFoundList to change grep with
reverse order
end if
(* RELOCK THE PREVIOUSLY LOCKED FRAMES THAT
WERE UNLOCKED FOR PROCESSING. *)
repeat with j from 1 to count items of
vParentFrameList
tell item
j
of vParentFrameList
if item j of
vWasLockedStatusList is
true then
set locked to true
end tell
set end of vResultList to
{vFoundList,
vParentFrameList, vWasLockedStatusList}
end repeat
end repeat
end
if
end tell
end repeat
set include locked layers for find of vFindChangeOptions to vOriginalLockedLayersSetting
return vResultList
end
tell
--
___________________________________________________
RICK GORDON
EMERALD VALLEY GRAPHICS AND CONSULTING
___________________________________________________
WWW: http://www.shelterpub.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden