Find and Paste Apple Script - replace

I have found an AppleScript below that finds and replaces... However instead of replacing with "401" in this case I want it to replace with what is on the clipboard.
So I am trying to put something like: set stringToReplace to keystroke "v" using command down...But that doesn't work (Expected end of line, etc. but found identifier.)
Here is the code:
on run {input, parameters}
set stringToFind to "404"
set stringToReplace to "401"
set theFile to choose file
set theContent to read theFile as «class utf8»
set {oldTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, stringToFind}
set ti to every text item of theContent
set AppleScript's text item delimiters to stringToReplace
set newContent to ti as string
set AppleScript's text item delimiters to oldTID
try
set fd to open for access theFile with write permission
set eof of fd to 0
write newContent to fd as «class utf8»
close access fd
on error
close access theFile
end try
return input
end run

Solved the issue:
All I needed to do was add: the clipboard after set stringToReplace to
on run {input, parameters}
set stringToFind to "404"
set stringToReplace to the clipboard
set theFile to choose file
set theContent to read theFile as «class utf8»
set {oldTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, stringToFind}
set ti to every text item of theContent
set AppleScript's text item delimiters to stringToReplace
set newContent to ti as string
set AppleScript's text item delimiters to oldTID
try
set fd to open for access theFile with write permission
set eof of fd to 0
write newContent to fd as «class utf8»
close access fd
on error
close access theFile
end try
return input
end run

Related

Read the contents in a text file and replace it with specified text in another text file using VBS

I've been trying to create a VBS script to read the contents of text in one text file and replace it with specified text located in another text file.
I'm trying to get the following script to read the contents in a file called first.txt. Once it does that its supposed to replace the specified text in a file called second.txt with the content read from the file called first.txt. This is what i have so far. Its replacing all text in the second.txt file instead of replace the specified text. Any ideas on this before i pull out my last strand of hair.Thanks!
Const ForReading = 1
Const ForWriting = 2
Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("first.txt",1)
strFileText = objFileToRead.ReadAll()
objFileToRead.Close
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "#sec", strFileText)
Set objFile = objFSO.OpenTextFile("second.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close
Hey #GeertBellekens your right and thank you for bringing this up. I was going to just correct this. This previous script seemed to have only read the second line from the first.txt file so I made minor changes in variable deceleration and reading the text by specified line. Here is the final working script.Its reading the first line of text in the first.txt file and replacing it with the "#sec" text located in the second.txt file. Let me know if it works for you. Thanks!!.
Here is the working code:
Option Explicit
Dim fso,strFilename,strSearch,strReplace,objFile,oldContent,newContent,strpw,objfso,strText,strNewText,filename,f,i,strline
filename = "first.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filename)
For i = 1 to 0
f.ReadLine
Next
strLine = f.ReadLine
Wscript.Echo strLine
f.Close
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("second.txt", ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "#sec", strLine)
Set objFile = objFSO.OpenTextFile("second.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close

How to read data from csv file that has line breaks

I have example data in csv file
Some text
Text 2
Text 3
I want to output it with line break into text area, but getting error when trying to put it as it is.
it works only when i remove line breaks
iimPlayCode('ADD !EXTRACT {{!COL1}}\n');
var description = iimGetExtract(1);
Edit your csv file by adding double quotes:
"1. Some text2. Text 2 3. Text 3"
And run the code:
iimPlayCode('SET !DATASOURCE yourfile.txt\nADD !EXTRACT {{!COL1}}');
var description = iimGetExtract(1);

how to edit the string form csv in macro script

I have csv file, which is called containg list of urls and looks like this :
My script will take the screenshots for all these urls.
My code looks like this:
VERSION BUILD=8871104 RECORDER=FX
TAB T=1
TAB CLOSEALLOTHERS
SET !ERRORIGNORE YES
SET !TIMEOUT_PAGE 120
SET !DATASOURCE W:\url-list-with-change-in-page-size.csv
SET !DATASOURCE_COLUMNS 1
SET !DATASOURCE_LINE {{!LOOP}}
URL GOTO={{!COL1}}
WAIT SECONDS=2
SET VAR1 {{!COL1}}
SET VAR2 EVAL("var s=\"{{VAR1}}\"; s.replace("http://",""); ")
SCREENSHOT TYPE=Page FOLDER=C:\Users\iMacros\Downloads FILE={{VAR2}}
This gives me an error wrong format of SET command, line 13 (Error code: 910)
So basically,
I have
{{COL1}} = http://www.webcentral.com.au/marketing/search-engine-optimisation
and i want
{{$VAR1}} = www.webcentral.com.au_marketing_search-engine-optimisation
With the help of EVAL() i want to first remove http:// and then replace / with _.
Any suggestions please.
Thanks
SET VAR2 EVAL("'{{VAR1}}'.replace('http://','').replace(/\\//g,'_');")
There are two ways to do this. Easiest would be to add one more column in your CSV with the name you want for that url. That way you dont have you manipulate the data. If you must use EVAL, you need to escape certain characters.
This works for me:
SET !VAR1 "http://stackoverflow.com/questions/28024675/how-to-edit-the-
string-form-csv-in-macro-script/28063429#28063429"
PROMPT {{!VAR1}}
SET !VAR2 EVAL("var x=\"{{!var1}}\"; var y=x.replace(\"http://\",\"\"); y;")
PROMPT {{!VAR2}}

How can I import and use labels from one Stata file to the current?

I have file aa with a variable x which is labeled with value label x_lab. I would like to use this value label on the variable x of Stata file bb:
use bb, clear
label value x x_lab
How can I import the value label x_lab?
You can use label save, which saves value labels in a do-file:
label save x_lab using label.do
use bb, clear
do label.do
See Stata help for label.
This answer technique didn't work for me as I wanted the variable labels created with e.g. label var connected "connected household", not the value labels.
Instead I used this advice: http://statalist.1588530.n2.nabble.com/st-How-to-export-variables-window-td3937733.html
*************
sysuse auto, clear
log using mylog, name(newlog) replace
foreach var of varlist _all{
di _col(3) "`var'" _col(20) "`:var label `var''"
}
log close newlog
//translate from proprietary format
translate mylog.smcl mylog.txt, replace
!start mylog.txt
*************
To fix the labels that extended over multiple lines so they just used a single one, I then replaced the \n > for the oversized labels with nothing (in regex mode in atom). I could easily save into TSV from there.
Specifically:
Clean up header and footer text in the logfile output.
On Mac: use "\n" instead of "\r\n".
On Windows: first "\r\n -> ""
then whitespace at beginning "\r\n " --> "\r\n"
then convert whitespace with 3 or more spaces in middle to tabs " +" --> "\t"
(Edit manually additional errors on tab if there are still some left)
save as mylog.tsv
open in Excel, and use table of labels as needed.

Trying to take a list and write it to a file as CSV using Applescript

Update Solution Found See Bottom of post if interested Seems simple enough and for the majority of the list it works, but at some point it messes up royally. The data for the list is coming from an asset in InDesign, but that is my end result and can be substituted for the sake of testing for a list defined within AS.
tell application "Adobe InDesign CS5.5"
set myagents to (name of properties of every asset of library "Agents.indl" as list)
end tell
set agentlist to ""
repeat with currentagent in myagents
set agentlist to agentlist & currentagent & ","
end repeat
set x to count agentlist
set agentlist to characters 1 thru (x - 1) of agentlist as string
my write_agents(agentlist)
on write_agents(this_list)
set the agent_log to ("/Volumes/Agents/agents.txt" as POSIX file)
try
set eof of agent_log to 0
open for access file the agent_log with write permission
write (this_list) to file the agent_log starting at eof
--set read_data to (read the_file as list) as text
close access file the agent_log
on error
try
close access file the agent_log
end try
end try
end write_agents
Instead of the Indesign tell at the top for testing we can use, the result is the same
set myagents to {"Lois Chase Johnson", "Tammy Newton", "Team Schmidt", "Tanya Kerr", "Terry Peterson", "Brenda Clark", "Deb Kahle", "Carol Dana", "Sheryl Burley & Cathy Reed", "Linda Ulin", "Dianna Erickson", "Dave Sharman", "Lori Tracey & Chuck Murphy", "Carolyn & Robert Dodds", "Jean Ryker", "Bryan Diehl", "Lois Scheck", "Nancy Rathke", "Patrick French", "Laura Halady", "Jennifer Felton", "Clarice Arakawa", "Holly Coburn", "Quint Boe", "Michaelle Barnard", "Paul Beck", "Kelly Johnson", "Doc Reiss", "Helga Filler", "Harriet Reyenga", "Thelma Durham", "Brooke Nelson", "Dick Pilling", "David Ramey", "Rebecca Jackson", "Tim Riley", "Pili Meyer", "Marc Thomsen", "Kathy Brown", "Jean Irvine", "Kathy Love", "Kimi Robertson", "Ed Sumpter", "Jo Cummins", "Kim Bower", "Mike Fuller", "Cathy Reed", "Joyce Underwood", "Daphne Eshom", "Kathy Brown", "Dave Ramey", "Mark DeRousie", "Tom Blore", "Paul Beck", "Kelly Johnson", "Alan Burwell", "Sheryl Burley", "Vivian Landvik", "Mark HcHugh", "Jim Newton", "Chuck Turner"}
What is in the text file?
Lois Chase Johnson,Tammy Newton,Team Schmidt,Tanya Kerr,Terry Peterson,Brenda Clark,Deb Kahle,Carol Dana,Sheryl Burley & Cathy Reed,Linda Ulin,Dianna Erickson,Dave Sharman,Lori Tracey & Chuck Murphy,Carolyn & Robert Dodds,Jean Ryker,Bryan Diehl,Lois Scheck,Nancy Rathke,Patrick French,Laura Halady,Jennifer Felton,Clarice Arakawa,Holly Coburn,Quint Boe,Michaelle Barnard,Paul Beck,Kelly Johnson,Doc Reiss,Helga Filler,Harriet Reyenga,Thelma Durham,Brooke Nelson,Dick Pilling,David Ramey,Rebecca Jackson,Tim Riley,Pili Meyer,Marc Thomsen,Kathy Brown,Jean Irvine,Kathy Love,Kimi Robertson,Ed Sumpter,Jo Cummins,Kim Bower,Mike Fuller,Cathy Reed,Joyce Underwood,Daphne Eshom,Kathy Brown,Dave Ramey,Mark DeRousie,Tom Blore,Paul Beck,Kelly Johnson,Alan Burwell,Sheryl Burley,Vivian Landvik,Mark HcHugh,Jim Newton,Chuck Turner �C�o�b�u�r�nutxt����Q�u�i�n�t� �B�o�eutxt���"�M�i�c�h�a�e�l�l�e� �B�a�r�n�a�r�dutxt����P�a�u�l� �B�e�c�kutxt����K�e�l�l�y� �J�o�h�n�s�o�nutxt����D�o�c� �R�e�i�s�sutxt����H�e�l�g�a� �F�i�l�l�e�rutxt����H�a�r�r�i�e�t� �R�e�y�e�n�g�autxt����T�h�e�l�m�a� �D�u�r�h�a�mutxt����B�r�o�o�k�e� �N�e�l�s�o�nutxt����D�i�c�k� �P�i�l�l�i�n�gutxt����D�a�v�i�d� �R�a�m�e�yutxt����R�e�b�e�c�c�a� �J�a�c�k�s�o�nutxt����T�i�m� �R�i�l�e�yutxt����P�i�l�i� �M�e�y�e�rutxt����M�a�r�c� �T�h�o�m�s�e�nutxt����K�a�t�h�y� �B�r�o�w�nutxt����J�e�a�n� �I�r�v�i�n�eutxt����K�a�t�h�y� �L�o�v�eutxt����K�i�m�i� �R�o�b�e�r�t�s�o�nutxt����E�d� �S�u�m�p�t�e�rutxt����J�o� �C�u�m�m�i�n�sutxt����K�i�m� �B�o�w�e�rutxt����M�i�k�e� �F�u�l�l�e�rutxt����C�a�t�h�y� �R�e�e�dutxt����J�o�y�c�e� �U�n�d�e�r�w�o�o�dutxt����D�a�p�h�n�e� �E�s�h�o�mutxt����K�a�t�h�y� �B�r�o�w�nutxt����D�a�v�e� �R�a�m�e�yutxt����M�a�r�k� �D�e�R�o�u�s�i�eutxt����T�o�m� �B�l�o�r�eutxt����P�a�u�l� �B�e�c�kutxt����K�e�l�l�y� �J�o�h�n�s�o�nutxt����A�l�a�n� �B�u�r�w�e�l�lutxt����S�h�e�r�y�l� �B�u�r�l�e�yutxt����V�i�v�i�a�n� �L�a�n�d�v�i�kutxt����M�a�r�k� �H�c�H�u�g�hutxt����J�i�m� �N�e�w�t�o�nutxt����C�h�u�c�k� �T�u�r�n�e�rutxt����
So it goes well for a while but at the end there are no commas but instead gobblygook which is unusable. Where have I gone wrong?
Not sure what exactly I changed, but it now works.....
tell application "Adobe InDesign CS5.5"
set myagents to (name of properties of every asset of library "Agents.indl" as list)
end tell
set agentlist to ""
set myagents to my simple_sort(myagents)
repeat with currentagent in myagents
set agentlist to agentlist & (currentagent) & ","
end repeat
set x to count agentlist
set agentlist to characters 1 thru (x - 1) of agentlist as string
my write_agents(agentlist)
on write_agents(this_list)
set the agent_log to ("/Volumes/RealEstate/agents.txt" as POSIX file)
try
set eof of agent_log to 0
open for access file the agent_log with write permission
write (this_list) to file the agent_log starting at eof
close access file the agent_log
on error
try
close access file the agent_log
end try
end try
end write_agents
on simple_sort(my_list)
set the index_list to {}
set the sorted_list to {}
repeat (the number of items in my_list) times
set the low_item to ""
repeat with i from 1 to (number of items in my_list)
if i is not in the index_list then
set this_item to item i of my_list as text
if the low_item is "" then
set the low_item to this_item
set the low_item_index to i
else if this_item comes before the low_item then
set the low_item to this_item
set the low_item_index to i
end if
end if
end repeat
set the end of sorted_list to the low_item
set the end of the index_list to the low_item_index
end repeat
return the sorted_list
end simple_sort