Data entry with Automator/Applescript? - macros

I have a list of a few hundred members in .txt format (one memberID per line) and I need to add them to a web app using Automator.
So the txt is something like:
30335842
30335843
30335844
...
And I need to insert this on a web page, but I guess thats the easy part because I can create actions using automator.
Just not sure how to get new id from the text file each time to use with the automator workflow.
Many thanks for your help.

It's easy, you basically read the file and put the results into a list. Then you can get the items in the list either referencing a list number directly...
set filePath to (path to desktop as text) & "memberID.txt" -- path to the file
set idsText to read file filePath -- get the file text
set idsList to paragraphs of idsText -- turn the text into a list
set nextID to item 2 of idsList
or you can get at them all with a repeat loop...
set filePath to (path to desktop as text) & "memberID.txt" -- path to the file
set idsText to read file filePath -- get the file text
set idsList to paragraphs of idsText -- turn the text into a list
repeat with i from 1 to count of idsList
display dialog (item i of idsList)
end repeat

Related

How to pass argument for the apple script

I have following script :)
property word_docs : {"org.openxmlformats.wordprocessingml.document", "com.microsoft.word.doc"}
property default_path : (path to desktop) as alias
property Delim : {".docx", ".doc"}
property PDF : ".pdf"
set outPDF to {}
set selected_files to (choose file of type word_docs default location default_path with multiple selections allowed without invisibles and showing package contents)
set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, Delim}
repeat with afile in selected_files
copy item 1 of text items of (afile as text) & PDF to the end of outPDF
end repeat
set AppleScript's text item delimiters to TID
tell application id "com.microsoft.Word"
activate
repeat with i from 1 to count of selected_files
open (item i of selected_files)
set theOutputPath to (item 1 of outPDF)
-- close access (open for access exportDocument)
tell active document
save as it file name theOutputPath file format format PDF
close saving no
end tell
end repeat
end tell
return
That helps me convert doc & docx files -> pdf, but it is too interactive.
And I have idea to run this script via terminal and i want to pass file path or directory as argument
for example:
$ script /Users/test/dest_dir/ /Users/test/out_dir/
will produce all pdf files into out_dir.
I saw this library also but it converts only docx files:
https://pypi.org/project/docx2pdf/
Is there anyone here who can help me rewrite this script .. I don't understand this language at all. or maybe someone will point to the finished tool. I need to do this on the mac os operating system.
see http://hints.macworld.com/article.php?story=20050523140439734
argv is an array of arguments in string format
on run argv
return item 1 of argv

How to add . to a name of a bookmark in Word

So i am using Word to create a template for the generation of a .pdf using XDocReport in Java. The problem i have encountered is that when i add a bookmark to an image in Word the allowed names cannot contain "." , for example i cannot name the bookmark estimate2.Photo , which is the name needed to correctly map the variable to the virtual object being passed. My question is the following, is there any way i can alter this default Word behaviour in order for it to allow me to add bookmarks that contain special characters like "." ?
I have found a way to rewire this default behaviour by modifying the configuration .xml files. In order to access the bookmarks file follow these steps:
Go to the .docx file right-click on it go to 7zip and open as archive.
Access the word folder and there you will find the document.xml file.
extract it
Open with notepad or any other text editor, find the bookmark start xml tag which contains the name property and modify it.
Once the file has been modified drag it back inside the archive and save.
Wow. I wouldn't have thought this would work... Nice find #Patratel
not withstanding the fact that it seems to work, I wouldn't recommend doing this for anything other than a temporary file or curiosity...
That said, here's the steps to do / test this:
The manual approach:
Add a regular bookmark (shortcut: Alt > I > K)
give it a name like dot_dot
save the file
change the extension from .docx to .zip
open the .zip folder
save document.xml to the desktop (or somewhere)
Find the text dot_dot and replace with dot.dot
save .xml file
copy .xml file back to .zip folder
rename .zip back to .docx
Open in MS Word
open bookmarks dialog (Alt > I > K)
select dot.dot from list
press Goto
A quick test of functionality
From there it is easy enough to test whether or not the bookmark can be used as normal... To that end I added a new paragraph and inserted a cross reference back to the bookmarked text. The cross reference worked.
The .InsertXML approach
Next I was also curious about #Cindy's comment about Range insert XML... It worked, to test this I used the following code in the immediate window:
' replace text in document with the equivalent XML (generates a few pages)
selection.Range.Text = thisdocument.Range.WordOpenXML
' replace the XML with the result of inserting itself into the document
thisdocument.Range.InsertXML thisdocument.Range.Text
An Automatic Approach
Sub dottyBM()
Dim newDoc As Word.Document
Set newDoc = Application.Documents.Add
newDoc.Range.Text = "Testing a dot bookmark"
Dim bmRange As Word.Range
Set bmRange = newDoc.Paragraphs(1).Range
bmRange.Start = bmRange.Start + InStr(bmRange.Text, " dot")
bmRange.End = bmRange.Start + 3
' bmRange.Bookmarks.Add "dot.dot" ' Err: 5828, Bad bookmark name
bmRange.Bookmarks.Add "dot_dot"
bmRange.InsertXML Replace$(bmRange.WordOpenXML, "dot_dot", "dot.dot")
Application.Dialogs(168).Show
newDoc.Close False
End Sub

Automate a Grep Applescript to Word Document

I'm using a Mac and I'm preparing accounts for a company. Every payslip which I've made in Microsoft Word has a voucher number. Because a transaction was missed all voucher numbers are wrong so now there are hundreds of wrong payslips. I want to create a script that can find the following GREP (find beginning of paragraph, text:Vch, any character until \r):
^Vch.+\r
and replace it with nothing (thereby deleting the whole sentence).
I was thinking of using Applescript as it can open the document, perform the GREP find (tricky part), save the document and save it as a pdf (all which is needed).
But apparently my knowledge fails me. Commands from the dictionary like create range, execute find, all bring errors.
Somebody experienced in Applescript that could help me devise a script? Any suggestions? It should be something like:
Tell application "Microsoft Word"
tell active document
set myRange to create range start 0 end 0
tell myRange
execute find find "^Vch.+\r" replace with ""
end tell
end tell
end tell
Many thanks!
There are no special characters to indicate the beginning of a line.
To search at beginning of the paragraph, the script must use return & "some text"
You can use "^p" as paragraph mark, but it doesn't work when you set the match wildcards property to true
To match an entire paragraph, the script must use return & "some text" & return, and the script must use replace with return to delete one paragraph mark instead of two.
Because the first paragraph does not begin with a paragraph mark, the script must use two execute find commands.
The wildcard is *
tell application "Microsoft Word" -- (tested on version 15.25, Microsoft Office 2016)
-- check the first paragraph
select (characters of paragraph 1 of active document)
execute find (find object of selection) find text ("Vch*" & return) replace with "" replace replace one wrap find find stop with match wildcards and match case without match forward and find format
--to search forward toward the end of the document.
execute find (find object of selection) find text (return & "Vch*" & return) replace with return replace replace all wrap find find continue with match wildcards, match case and match forward without find format
save active document
-- export to PDF in the same directory as the active document
set pdfPath to path of active document & ":" & (get name of active window) & ".pdf"
set pdfPath to my createFile(pdfPath) -- create an empty file if not exists, the handler return a path of type alias (to avoid grant access issue)
save as active document file name pdfPath file format format PDF
end tell
on createFile(f)
do shell script "touch " & quoted form of POSIX path of f
return f as alias
end createFile

assign a specific style to multiple word files without opening them

I created a new style in word(through "create a style...") and I want to assign this style to 200 word files. it is very time consuming to open them one by one and assign the style to them. Is there any way to assign the style to them without opening them?
The easiest way to do this is using Word VBA.
Place the 200 Word files that will be assigned the style into a directory that contains no other files. Then create a .dotm (Word 2007 or higher) or .dot (Word 2003 or lower) template file in a different location. Create the style to be copied in the template file and also place the following code into a module within that same template file (ALT-F11 to access the editor):
Sub BatchCopyStyles()
'Make sure that the template that contains the style to be copied and this code
'is open and acting as the active document before running this macro
Dim file As Variant
Dim folderPath As String 'path to files receiving the style
Dim targetPath As String
Dim templateFile As String 'file that contains style and this code
Dim styleTemplate As Document
folderPath = "C:\Users\Joe\Desktop\TargetFolder\"
templateFile = "C:\Users\Joe\Desktop\CopyStyle.dotm"
Set styleTemplate = ActiveDocument
file = Dir(folderPath)
While (file <> "")
Set file = Documents.Open(FileName:=folderPath & file)
styleTemplate.Activate
targetPath = folderPath & file
Application.OrganizerCopy Source:=templateFile, _
Destination:=targetPath, _
Name:="StyleToCopy", _
Object:=wdOrganizerObjectStyles
file.Close wdSaveChanges
file = Dir
Wend
End Sub
Edit the code for the correct paths, file name, style name, etc. With the file containing this code and the style to be assigned as the Active Document, run the macro from the VBA editor (F5). This will open each file, copy the style, and then close the file. Opening and closing a document 200 times won't be pretty, but it should do the job.

Can I make a macro in n++ that does a search/replace?

I'm new to n++, but I have been most impressed with this tool so far. I've been trying to record a macro that do a search/replace, but the 'search' part seems to have the initial search text from the recording 'hard-coded' in the macro.
What I want is:
Manually locate the cursor at the beginning of the first line of a fixed format code segment, then Macro actions:
move cursor two lines down
move cursor right x characters
mark charters from pos x to x+n
search and replace all occurrences of the selected text with "{p_'selected text'}"
In an more advanced version, I'd like to add some logic to step 4: only execute the replace part if the # of occurrences are > 1 (e.g. by first adding a count statement, but I'm not sure how to obtain the returned count # from the dialog box)
Is this possible?
While I'm a big fan of Notepad++, this sounds like something I would accomplish with AutoHotKey. You would select the text and copy it to the clipboard. AutoHotKey would read the clipboard, replace the text as you desire, and either replace the clipboard contents, or send it back to your document. Let me know if you would like to go that route.