Apply to Only 1 current document FInd and change - applescript

Hi Below is apple script which does find and change operation in indesign document.But this script does not execute and gives a error.
"Adobe InDesign CS6 got an error: Can’t set find text preferences of document 1 to nothing."
script is
tell application "Adobe InDesign CS6"
tell front document
set find text preferences to nothing
set change text preferences to nothing
--Letter Z--
set case sensitive of find change text options to true
set abbreviation to ".<005A>"
set thinspace to ". <005A>"
set find what of find text preferences to abbreviation
set change to of change text preferences to thinspace
change text
set find text preferences to nothing
set change text preferences to nothing
end tell
end tell
Please suggest any one.
This should run for only for opened document. when front document is removed this script runs.
I could not sort out the error.

The find text preferences and change text preferences belong to the application, not to a document. Try this
tell application "Adobe InDesign CS6"
if (count documents) is 0 then return
set find text preferences to nothing
set change text preferences to nothing
set case sensitive of find change text options to true
set abbreviation to ".<005A>"
set thinspace to ". <005A>"
set find what of find text preferences to abbreviation
set change to of change text preferences to thinspace
tell document 1
change text
end tell
set find text preferences to nothing
set change text preferences to nothing
end tell

Related

AppleScript cannot save a textedit file

I'm using a code to format textedit files for lab data.
I currently am using AppleScript to create a document in TextEdit and adding text to it, but when I try to save it, TextEdit gives me the error "you do not have permission to save as blahblahblah." I have already tried changing the permission on the folder in which I'm saving it in, but I think it may have something to do with it being a file that AppleScript created.
The exact error output is a dialog box from TextEdit
The document “1.txt” could not be saved as “1”. You don’t have permission.
To view or change permissions, select the item in the Finder and choose File > Get Info.
The segments of my code that don't work are
tell application "TextEdit"
make new document with properties {name:("1.txt")}
end tell
--data formatting code here (n is set here)
tell application "TextEdit"
delay 1
close document 1 saving in ("/Users/bo/Desktop/Script Doc/" & (n as string))
set n to n + 1
make new document with properties {name:((n as string) & ".txt")}
delay 1
end tell
I have scoured other questions and I have found the code segments
open for access document 1
close access document 1
but I'm not sure how to implement these / if I even should, and if not, I'm not sure how to fix this issue.
Thanks in advance
Unfortunately, that error message isn't helping much. You are essentially trying to save into a string, which isn't going to work - you need to use a file specifier, for example:
close document 1 saving in POSIX file ("/Users/bo/Desktop/Script Doc/" & n & ".txt")
Note that not everything knows about POSIX paths, in which case you will need to coerce or specify it as in my example.
Saving directly to the Desktop works for me using the latest version of macOS Mojave
property outputPath : POSIX path of (((path to desktop as text) & "Script Doc") as alias)
property docNumber : 1
property docExtension : ".txt"
tell application "TextEdit"
set docName to (docNumber & docExtension) as text
set theText to "Your Desired Text Content"
set thisDoc to make new document with properties {name:docName, text:theText}
close thisDoc saving in POSIX file (outputPath & "/" & (name of thisDoc))
(* IF YOU WANT TO SAVE MULTIPLE FILES WITH CONSECUTIVE NAMES *)
set docNumber to docNumber + 1
set docName to (docNumber & docExtension) as text
set thisDoc2 to make new document with properties {name:docName} -- Removed Text Property This Time
close thisDoc2 saving in POSIX file (outputPath & "/" & (name of thisDoc2))
end tell
TextEdit is sandboxed. You cannot save documents in the standard desktop folder.
An alternative is to hard-code the path to the documents folder in the container of TextEdit.
tell application "TextEdit"
make new document with properties {name:"1.txt"}
end tell
set containerDocumentsFolder to (path to library folder from user domain as text) & "Containers:com.apple.TextEdit:Data:Documents:"
tell application "TextEdit"
close document 1 saving in containerDocumentsFolder & (n as string) & ".txt"
set n to n + 1
make new document with properties {name:(n as string) & ".txt"}
end tell

Change the color of text by using applescript

I am writing some text in to word file i want to change the color of that text any one can help on that one plz.
I want to print the 'message' from following script in red color.
Here is the Script:
set message to "mostly these windows are popup in application"
on ResultCreationFuction(message)
try
set text_to_save to message as text
tell application "System Events"
tell application "Finder"
set sortedList to sort (get files of folder "SofTestAutomationResult" of desktop) by modification date
set FileCount to get count of sortedList
set theFile to (item FileCount of sortedList) as alias
end tell
set file_ref to open for access theFile with write permission
write (text_to_save & return) to the file_ref starting at eof
close access file_ref
delay 2
end tell
end try
end ResultCreationFuction
Some Details:
The file is word which is all ready present on above location having name "10.012.2014_17_4_20.doc" (the name of .doc file is not fix)
What you are attempting is the wrong way to do it.
To manipulate content like that, including formatted text (not plain
text), you need to work within, ideally, a well-scriptable app, like
Pages (or Word, perhaps, but I don't have that on the machine I'm
writing this from).
Don't use System Events if you don't need to. Use the apps with the appropriate AppleEvents/dictionary, etc. If you don't know what I'm talking about, you need to take advantage of the infinite resource known as the web.
"Fuction" is just bad form.
I would suggest doing a lot more reading up on how AppleScript works (or scripting in general), but to start you out, here is a script I just wrote in pages which sets the color of a specific word of the open document after putting text in there:
tell application "Pages"
set body text of document 1 to "hello there mister fancy pants"
set color of word 3 of body text of page 1 of document 1 to {64614, 0, 111}
end tell
If you have Pages, try this by starting with a blank page and running this script. Obviously, you could get rid of "word 3 of" in the 2nd line, and the whole body text will be red.
I hope this makes sense and is of help.
[edit]
I should mention that even TextEdit is scriptable and can open Word documents. Here's an example using TextEdit:
tell application "TextEdit"
set text of document 1 to "hello mister fancy pants"
set color of words 2 thru 3 of text of document 1 to {65535, 0, 0}
end tell
There is a little danger of non-Word apps losing formatting of Word files. But it just seems you are attempting something very simple, and I'm not sure if Word is really necessary here.
You can't add color using the write to eof. You should open the document in Word and then insert the line and add the color. Here's a script that should demonstrate how:
set text_to_add to "mostly these windows are popup in application"
set theFile to ((path to desktop folder) & "10.012.2014_17_4_20.doc") as string
tell application "Microsoft Word"
set theFile to theFile as string -- assuming theFile is an alias or :: path
open file theFile
tell active document
set endOfDoc to end of content of text object -- insert the text to end of document
set theRange to create range start (endOfDoc - 1) end endOfDoc
insert text text_to_add at theRange
set myRange to create range start endOfDoc end (endOfDoc + (length of text_to_add))
set color index of font object of myRange to red
save
end tell
end tell

XCode 5 - AppleScript - How to get document in current tab

I want to open a document in the current tab in an external application (MacVim for example). Based on a StackOverflow answer I created an Automator service with following AppleScript code:
tell application "Xcode"
set current_document to last source document
set current_document_path to path of current_document
end tell
tell application "MacVim"
activate
open current_document_path
end tell
The issue is, that it opens the file from the first tab and not from the current tab. How can I get the path of the current tab?
Following workaround based on SO answer works for me.
As noted in the comment there: This works EXCEPT if you are using the Assistant editor - then you end up with whatever happens to be in the Standard Editor. – Lloyd Sargent but for me it's better than the first tab.
on run {input, parameters}
set current_document_path to ""
tell application "Xcode"
set last_word_in_main_window to (word -1 of (get name of window 1))
if (last_word_in_main_window is "Edited") then
display notification "Please save the current document and try again"
-- eventually we could automatically save the document when this becomes annoying
else
set current_document to document 1 whose name ends with last_word_in_main_window
set current_document_path to path of current_document
end if
end tell
tell application "MacVim"
if (current_document_path is not "") then
activate
open current_document_path
end if
end tell
return input
end run
try the following
tell application "Xcode"
set docs to source documents
if length of docs is less than 1 then
display dialog "for ....... I need at least 1 document"
return
end if
path of last item of source documents
end tell

Export active page to PDF (AppleScript + InDesign)

I have an AppleScript that exports the active document in InDesign to a PDF on my desktop.
--this script exports all opened InDesign document as PDFs
set my_destination to (path to desktop) as string
tell application id "com.adobe.InDesign"
activate
--next line collects all current PDF export presets
set my_pdf_export to get the name of every PDF export preset
--next line makes you choose which preset to use for ALL opened InDesign documents
set my_pdf_choice to (choose from list my_pdf_export) as string
--loop starts here
repeat with this_doc in (active document)
--next line gets the name of the active InDesign document
set doc_name to get name of this_doc
--next 3 lines perform the actual PDF export
tell this_doc
export format PDF type to my_destination & (characters 1 thru -6 of doc_name) & ".pdf" using my_pdf_choice without showing options
end tell
end repeat
end tell
The script prompts for a PDF preset selection before exporting.
I'd like the script to export only the active page of the active document.
I've tried a couple things like replacing "active document" with active page", however I am not too knowledgable about AppleScript.
Any ideas?
It looks like you got your code from This Macscripter question, and the code is already in that script. You just need to set the page range of InDesign's PDF Export Preferences
tell PDF export preferences
set page range to "1, 3-5"
end tell
Just make sure to set it back at the end, because this is a persistent application setting.
tell PDF export preferences
set page range to all pages
end tell

How to read a file and open Safari with the read result with Mac (AppleScript)?

I need to open multiple Safari (or open the tab is OK) based on the read result.
For example, if a file has
http://a.com
http://b.com
I want to open a.com and b.com using Safari.
How can I do that with Mac/AppleScript?
Maybe I can run python calling "open -a Safari "http://a.com", but I guess AppleScript is the tool for this kind of job.
not sure about python but this will read a text file and open windows let me see if I can get tabs for you though
set locations to paragraphs of (read (choose file with prompt "Pick text file containing urls"))
repeat with aline in locations
if length of aline is greater than 0 then
tell application "Safari"
make new document at end of documents
set URL of document 1 to aline
end tell
end if
end repeat
EDIT:
Ok this is better and it opens them in tabs of a single window
set locations to paragraphs of (read (choose file with prompt "Pick text file containing urls"))
tell application "Safari"
activate
set adoc to make new document
end tell
repeat with aline in locations
if length of aline is greater than 0 then
tell application "Safari" to make new tab at end of window 1 with properties {URL:aline}
end if
end repeat
New Addtion
this is yet another way based on regulus6633's post in conjunction with mine
set locations to paragraphs of (read (choose file with prompt "Pick text file containing urls"))
repeat with aLocation in locations
tell application "Safari" to open location aLocation
end repeat
If you want it to specifically open the links in Safari then mcgrailm's solution is good. However, you don't need the Finder for the first part so take that code out of the Finder tell block. There's no need to tell the Finder to do something that applescript can do itself.
However, you probably want to open the links in whatever browser is the user's default browser. It may be Safari or Firefox etc. You can do that with the "open location" command. So something like this is probably what you want...
set theFile to choose file with prompt "Pick text file containing urls"
set locations to paragraphs of (read theFile)
repeat with aLocation in locations
try
open location aLocation
end try
end repeat

Resources