Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
7

Apple Script GREP to format phone numbers - help needed

Explorer ,
May 05, 2024 May 05, 2024

Every day, I receive files that use varying formats for phone numbers and I have to make them uniform. When I use the find and replace GREP within InDesign below, it works. However, I want to add this to a script I have that makes other normal find and replace statements, so I can do it in one push of the button.

 

Find what:  \(?(\d\d\d)\)?[-. ]?(\d\d\d)[-. ]?(\d\d\d\d)

Change to:  $1.$2.$3

______

Right now I have this (below) but it keeps returning this error:

 

tell application "Adobe InDesign 2024"

set find grep preferences to nothing

set find what of find grep preferences to "(?<(\\d{3}))?[-. ]?(\\d{3})[-., ]?(\\d{4})"

set change grep preferences to nothing

set change grep preferences to find grep preferences -- Set change grep preferences to the same as find grep preferences

set change grep to "$1.$2.$3"

tell active document

set myFoundItems to change grep

end tell

end tell

 

returns: 

Error Number: 30477

Error String: Adobe InDesign 2024 got an error: Invalid value for set property 'change grep preferences'. Expected change grep preference or nothing, but received find grep preference.

_____

 

This is my other attempt:

 

tell application "Adobe InDesign 2024"

set find grep preferences to nothing

set find what of find grep preferences to "(?<(\\d{3}))?[-. ]?(\\d{3})[-., ]?(\\d{4})"

set change grep preferences to nothing

set change grep to "$1.$2.$3"

tell active document

set myFoundItems to change grep

end tell

end tell

 

but I get this error:

Error Number: -10006

Error String: Can't set change grep of "$1.$2.$3" to «script».

----

I am an amateur but I am hoping that someone can help me figure out why this won't work and what will... Thank you.

TOPICS
How to , Scripting
1.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , May 05, 2024 May 05, 2024

I like your approach: a script to cleanup a bunch of things at once. That is why I have a Mikes Find/Change by List 2024 available on my website. It cleans up about 30 common typesetting things. I left out phone number cleanup because that is so preferential, but it could be edited in quite easily. Also, I prefer JavaScript over AppleScript, because I prefer cross-platform-ability.

Translate
Community Expert ,
May 05, 2024 May 05, 2024

Not sure why you want to script this when phone number cleanup is already in the drop-down of the find/change grep panel?

Mike Witherell
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 05, 2024 May 05, 2024

To be fair, I had no idea that preset was there. 🙂

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 05, 2024 May 05, 2024

I am hoping to help my coworker by writing a whole script that does about 8 different cleanup/find/replace commands. This is to help her run one script every morning instead of doing a separate action that she could easily forget. If I an automate it, it would help tremendously.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 05, 2024 May 05, 2024

You can use the GREP string in that preset as a model or starting point.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 05, 2024 May 05, 2024

Yes, this is where I'm getting the GREP strings, but I think something has to change to make Apple Script understand them. If I paste in the GREP directly, Apple Script chokes on the Syntax error: Expected “"” but found unknown token, when I compile.

 

GREP:  \(?(\d\d\d)\)?[-. ]?(\d\d\d)[-. ]?(\d\d\d\d)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 05, 2024 May 05, 2024
quote

error: Expected “"” but found unknown token, when I compile.

 

GREP:  \(?(\d\d\d)\)?[-. ]?(\d\d\d)[-. ]?(\d\d\d\d)


By @Venessa Sylvester

 

You need to escape certain reserved characters such as ", \ and some other.

 

The \ character is used for, well, escaping so you need to escape it in the string to indicate that \ is the actual character you wish to use in the string. For example, that's how this string should be passed to AppleScript:

 

"\\(?(\\d\\d\\d)\\)"

 

(If you want it to be explained more eloquently, just google "escaping characters in AppleScript").

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 05, 2024 May 05, 2024

Without going into further details, you can't "set change grep":

 

leor_0-1714927189706.png

 

"change grep" is a command:

 

leor_1-1714927244371.png

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 05, 2024 May 05, 2024

Thank you. This is helpful. I changed it to: 

set change to of change grep preferences to "$1.$2.$3"

but the whole thing doesn't run at all, even though it compiles after that change.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 06, 2024 May 06, 2024

If I have understood the problem, this may help. It works for me.

 

 

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set FindString to "\\(?(\\d\\d\\d)\\)?[-. ]?(\\d\\d\\d)[-. ]?(\\d\\d\\d\\d)"
set ReplaceString to "$1.$2.$3"
tell application id "com.adobe.InDesign"
	set GrepTarget to active document
end tell

RunGrep(GrepTarget, FindString, ReplaceString)


on RunGrep(GrepTarget, FindString, ReplaceString)
	tell application id "com.adobe.InDesign"
		-- Clear the Find/Change fields
		set find grep preferences to nothing
		set change grep preferences to nothing
		-- Set FindWhat/ChangeTo values
		set find what of find grep preferences to FindString
		set change to of change grep preferences to ReplaceString
		-- Set the find options
		set include footnotes of find change grep options to false
		set include hidden layers of find change grep options to false
		set include locked layers for find of find change grep options to false
		set include locked stories for find of find change grep options to false
		tell GrepTarget
			change grep
		end tell
		set find grep preferences to nothing
		set change grep preferences to nothing
	end tell
end RunGrep

  

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 05, 2024 May 05, 2024

I like your approach: a script to cleanup a bunch of things at once. That is why I have a Mikes Find/Change by List 2024 available on my website. It cleans up about 30 common typesetting things. I left out phone number cleanup because that is so preferential, but it could be edited in quite easily. Also, I prefer JavaScript over AppleScript, because I prefer cross-platform-ability.

Mike Witherell
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 06, 2024 May 06, 2024
LATEST

@Venessa Sylvester

 

As @Mike Witherell mentioned - maybe you could use Find Change by List script included with InDesign and just add your own "rules"? 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines