I want to add code and append it on runtime.
Example:
This code is before adding code on runtime
on opencard
emptyvariable
end opencard
command selectvdofile
answer file "Select Video file"
put it into fld "txt_namefile"
put it into namefile
end selectvdofile
This code is after adding code on runtime
on opencard
emptyvariable
-------------------------
put "test" into TestV << This line
end opencard
command selectvdofile
answer file "Select Video file"
put it into fld "txt_namefile"
put it into namefile
end selectvdofile
Anyone can show exmple code for me ?
Here's an example. Best to save your stack before testing!
on mouseUp
local tScript, tLine
put the script of card "MyCard" into tScript
put lineOffset("end opencard", tScript) into tLine
if tLine > 0 then
put format("put \"test\" into TestV") & LF before line tLine of tScript
set the script of card "MyCard" to tScript
end if
end mouseUp
Related
I want to create a Mac app similar to Textexpander or Atext. Both these applications allow the user to define snippets along with their respective trigger words. Typing the trigger words in any app, replaces that trigger word with the actual snippet defined.
I presume that the app listens to all strings being typed in any app and when it detects a string matching one of the trigger words defined, it replaces it with the snippet.
Is that how it actually works, or is there some other way?
Make two fields. In field 2 put something like:
time xyz
come ABC
In the script of field 1:
on textChanged
if the last char of me = space then
put the last word of me into temp
if temp is in fld 2 then
repeat for each word tWord in fld 2
put the last word of line lineOffset(temp,fld 2) of fld 2 into the last word of me
exit repeat
end repeat
end if
select after text of me
end if
end textChanged
Now type into fld 1, you know, "Now is the time for all good men to come to the aid of their country". This can be better done with an array, but the concept may be more accessible here.
This is a better handler, since it will not react to the trigger word:
on textChanged
if the last char of me = space then
put the last word of me into stringOfInterest
put fld 2 into dataToSearch
if stringOfInterest is in dataToSearch then
repeat for each line tLine in dataToSearch
if word 1 of tLine = stringOfInterest then
put word 2 of tLine into the last word of me
exit repeat
end if
end repeat
end if
select after text of me
end if
end textChanged
I am trying to build an AppleScript that reads each line of a “.txt” file (with a linefeed) and stores the contents of each line into AppleScript variables.
Here is what I mean:
Let’s say there was a “Test.txt” file with the contents:
Apples
Oranges
Pears
As you can see, the “Test.txt” file’s contents have a String on each line, a linefeed introducing the new String, and so on.
I would really like to know how an AppleScript could be made so that each line’s Strings are copied into individual AppleScript variables.
(This way, “Apples” in the first line, would be stored in variableA, “Oranges” in the next would be stored in variableB, “Pears” … variableC, etc.)
Please let me know, from your experience, how best to accomplish this. I know it’s slightly more involved, here is where I am:
(*
This portion of the AppleScript accesses the contents of the ".txt" file named "Test," though takes all of the content and places it into a single variable.
*)
set newFile to ("Macintosh HD:Users:Username:Desktop:Test.txt")
set theFileContents to (read file newFile)
{ AppleScript code to read each line to individual variables }
I know there must be others trying to accomplish this.
This example is for a situation where you know the anticipated paragraphs you'll be looking for to assign to each of a known set of variables.
set newFile to ("Macintosh HD:Users:Username:Desktop:Test.txt")
set theFileContents to paragraphs of (read file newFile)
set recipientEmail to paragraph 1 of theFileContents as text
set senderEmail to paragraph 2 of theFileContents as text
set theSubject to paragraph 3 of theFileContents as text
set theBody to (paragraphs 4 thru -1 of theFileContents) as text
Another option would be to dynamically search for a string in the paragraph, and if it matches, then assign it to that variable. Something like:
set newFile to ("Macintosh HD:Users:jweaks:Desktop:horses.txt")
set theFileContents to paragraphs of (read file newFile)
set recipientEmail to ""
set senderEmail to ""
set theSubject to ""
set theBody to ""
repeat with p in theFileContents
if p contains "To:" then
set recipientEmail to p
else if p contains "From:" then
set senderEmail to p
else if p contains "Subject:" then
set theSubject to p
else
set theBody to theBody & p & return
end if
end repeat
Thank you so much for all of your effort to answer this question, jweaks. As I am still catching on to AppleScript best practices, I was thinking more about your recommendation of bringing the contents of the “.txt” file into a list, assigning the items to AppleScript variables (if needed), and began brainstorming how to accomplish it. I agree that it seems like the simplest and most efficient approach:
Reads the “.txt” file’s into a list of items: Apples, Oranges, and Pears
set paragraph_list to read file "Macintosh HD:Users:tombettinger:Desktop:Test.txt" using delimiter linefeed
Apples
set variableA to item 1 of paragraph_list
Oranges
set variableB to item 2 of paragraph_list
Pears
set variableC to item 3 of paragraph_list
Displays the contents of each variable (optional comment)
display dialog variableA & " " & variableB & " " & variableC
End of AppleScript
As long as the contents of the ".txt" file are stacked in a table, this approach will support the accessibility of information I was searching for. Thanks again!
Here's the simple handler I'm using to test/learn the revZip library. The fld "XML" contains XML as text.
I just can't see the error... Any ideas?
command SaveIssue
put fld "XML" into tXML
ask file "Save as:" with "NewIssue.zip" as sheet; CheckResult the result
put it into tPath
revZipOpenArchive tPath, "write"; CheckResult the result
revZipAddItemWithData tPath, "issue.xml", tXML; CheckResult the result
revZipCloseArchive tPath; CheckResult the result
end SaveIssue
command CheckResult tResult
if tResult is not empty then
if tResult is "Cancel" then exit to top
answer "Error:" && tResult
exit to top
end if
end CheckResult
I found the answer in an old forum post. The revZip external requires quotes around variable names. Kind of weird, but this change to this line fixes it:
revZipAddItemWithData tPath, "issue.xml", "tXML"
I try to print PDF with HyperLink, without succes
I have a lock field "etat" with data
Data are formatted like:
Code TAB Nom TAB Adress TAB Ville
56 Eric rue caboteur ST NAZAIRE
87 Franc rue Midin ST brevin
etc
the textStyle of item 3 "Adress" set to "Link and linktext set to ""http://maps.google.fr/maps?f=q&hl=fr&q=theadresse""
When i click on hyperlink item in field "etat" that work fine
Now i would like have the same result after printing this field in PDF
I use this code for printing
on mouseUp
local theResult,thepDF
ask file "" with type "PDF file|pdf|PDF "
if it = "" then
exit mouseUp
end if
put it into thePDF
put the htmltext of fld "etat" into theResult
--set the fileType to "revoPDF "
open printing to pdf thePDF
revPrintField the name of field "etat"
--revPrintText theResult,"<%pageNumber%> of <%numPages%>","<%pageNumber%> of <%numPages%>",the long id of field "etat" of stack "CDH"
close printing
end mouseUp
I have a nice PDF with clicked words, but the click don't launch google maps
The dictionary say for command "print link"
"When printing fields, any text that has its linkText property set together with the link textStyle is treated as if a print link command had been executed with the contents of the property as link, and the formattedRect of the text as rectangle."
But no work.... I have a big headache
Thank for yours help to achieve this
Eric
I am trying to understand your problem and reading through your code, I think maybe the link text…
"http://maps.google.fr/maps?f=q&hl=fr&q=theadresse"
should be…
"http://maps.google.fr/maps?f=q&hl=fr&q=" & theadresse
This is assuming theadresse is a variable containing a valid address to search in Google.
I am currently using this Applescript I found that searches for a file name and returns the file path in a text doc. This works fine for finding 1 or 2 files, but I would like to find 500 files that are spread over hundreds of folders. My ideal script would use data from an excel spreadsheet or csv, perform a search, find the file and make a copy of it in a designated folder on my desktop. Any help is appreciated.
Here is the script I found:
tell application "System Events"
activate
set thePattern to text returned of (display dialog "Search for" default answer "")
end tell
if thePattern = "" then return
try
set foundFiles to do shell script "mdfind -name " & quoted form of thePattern & " | /usr/bin/egrep -i " & quoted form of thePattern & "[^/]*/?$ | /usr/bin/grep -vi " & quoted form of thePattern & ".*" & quoted form of thePattern
on error
set foundFiles to "Nothing Returned"
end try
if foundFiles = "" then set foundFiles to "Nothing Returned"
tell application "TextEdit"
activate
delay 0.5
try
set theDoc to document 1
get text of theDoc
if result is not "" then
make new document
set theDoc to result
end if
on error
make new document
set theDoc to result
end try
set text of theDoc to foundFiles
end tell
You need to read the data from the text file, then turn it into a return or linefeed delimited list and do a repeat over the items of this list. Then turn each item (which is actually a line) into e.g. a tab delimited list and again do a (nested) repeat loop over the items of this list. If you know that e.g. item 3 is the file path, you can set a variable to item 3 of the line as text and use this variable in your shell script.
I think you need to show that you understand the concept of repeat loops by posting your own attempt of implementing this. If you do, I'll be happy to come back and help you with the next step.
Kind regards,
Mark