Im Trying to get Apple Script to Read A file and tell me if certain key words are in there - file

I've gotten this far but I don't know what to do next if you know.
Can you please make edits to the script and then post the script back?
on TerrorChecker()
tell application "Finder"
read ("/ Users / BenEvans / desktop / Test")
if "Blue,Red,Yellow" then display dialog "Terror Alert" buttons {"OK"} default button 1
end tell
do
Nothing
end TerrorChecker
TerrorChecker()
end TerrorChecker

assumed there's a file contains the keyword "darkness", try this one:
set fileContent to (read (choose file))
if "darkness" is in fileContent then
display dialog "terror alert" buttons {"OK"} default button 1
else
say "no keyword found"
end if
btw, the command read cannot handle property file, e.g. *.pdf, *.xls
then try this one:
set fileContent to (read (choose file))
if "blue" is in fileContent then
display dialog "terror alert" buttons {"OK"} default button 1
else if "red" is in fileContent then
display dialog "terror alert" buttons {"OK"} default button 1
else if "yellow" is in fileContent then
display dialog "terror alert" buttons {"OK"} default button 1
else
say "no keyword found"
end if

Your example was a little bit of a mess, so I've modified it below.
on run
TerrorChecker()
end run
on TerrorChecker()
tell application "Finder"
set theData to read "/Users/BenEvans/Desktop/Test"
if theData contains "Blue,Red,Yellow" then
display dialog "Terror Alert" buttons {"OK"} default button 1
end if
end tell
end TerrorChecker

Related

Set Control Property after opening a form on a specific record

Im new to Access and need some help. D:
So I have a form in datasheet view which displays basic information of all the records from a query. By pressing a text field at the end of each record it opens another form in edit mode with the details of that specific record.
But after it opens I want to disable or hide some fields and controls including a button which clears all the fields in the form so that users can't press it accidentally and erase all Information in the form.
I've tried this code:
Private Sub Text23_Click()
DoCmd.OpenForm "FrmEntregas", acNormal, "", "[EntregasID]=" & Nz(CodeContextObject.EntregasID, 0), acEdit, acDialog
'Forms("FrmEntregas").btnReiniciar.Visible = False
'Forms!FrmEntregas!btnReiniciar.Visible = False
'Forms("FrmEntregas").Form.Controls(btnReiniciar).Visible = False
End Sub
The first line works great, the problem comes when refering to the form's button. I've tried refering to the button in three different ways but none of them work. It displays this message:
"Runtime Error 2450: Microsoft Access cannot find the referenced form 'FrmEntregas'."
If someone could point me in the right direction I'd be more than thankful!
Don't open in Dialog mode, the code after DoCmd works only when the form is closed. Change the Modal property of the form to Yes instead.
Private Sub Text23_Click()
DoCmd.OpenForm "FrmEntregas", acNormal, "", "[EntregasID]=" & Nz(CodeContextObject.EntregasID, 0), acEdit
Forms("FrmEntregas").btnReiniciar.Visible = False
End Sub

Applescript loop to parse CSV and inject values

I am trying to loop through each comma separated value in a file and have each item undergo a series of actions. Essentially the workflow is this:
CSV file:
123,456,789
Workflow:
[1] Take '123'
[2] Inject '123' into a text field
[3] Click on buttons
[4] Repeat until (and including) last value in CSV
My current code is as follows, but I'm not being able to correctly loop or inject the current item into the text field.
Any pointers you could give me?
set pathInputFile to (choose file with prompt "Select the CSV file" of type "csv")
set strFileContents to read pathInputFile
set parFileContents to (paragraphs of strFileContents)
set numRows to count of parFileContents
repeat with iPar from 1 to number of items in parFileContents
set lstRow to item iPar of parFileContents
if lstRow = "" then exit repeat -- EXIT Loop if Row is empty, like the last line
set lstFieldsinRow to parseCSV(lstRow as text)
----------
-- now I would need to pass the current value to the next block
-- and "paste" it into the text field
----------
tell application "System Events"
tell process "App"
set frontmost to true
click menu item "Query Window..." of menu "Network" of menu bar 1
click radio button "Number" of tab group 1 of window "Query"
set focused of text field 1 of tab group 1 of window "Query" to true
delay 0.1
keystroke "a" using command down
delay 0.1
keystroke "v" using command down
click UI element "Query" of group 4 of splitter group 1 of window "Query"
end tell
end tell
end repeat -- with iPar
EDIT:
set pathInputFile to (choose file with prompt "Select the CSV file" of type "csv")
set strFileContents to read pathInputFile
log strFileContents
Output:
(*147782
167482
182676
185309
184119
188494*)
What delimiter should I use in this case?
If the CSV is simple CSV (without double quotes as additional field separators) it's also simple to separate the items with AppleScript's text item delimiters.
set theCSV to "123,456,789"
set {TID, text item delimiters} to {text item delimiters, ","}
repeat with anItem in (get text items of theCSV)
display dialog anItem buttons {"OK"} default button 1
end repeat
set text item delimiters to TID
If all fields are wrapped in double quotes you can use this
set theCSV to "\"123\",\"456\",\"789\""
set trimmedCSV to text 2 thru -2 of theCSV
set {TID, text item delimiters} to {text item delimiters, quote & "," & quote}
repeat with anItem in (get text items of trimmedCSV)
display dialog anItem buttons {"OK"} default button 1
end repeat
set text item delimiters to TID
The data structure in the EDIT part seems to be line separated (one item per line) not really CSV. In this case neither text item delimitersnor a repeat loop is needed. paragraphs of reads the text line separated (it considers LF, CR and CRLF).
set strFileContents to paragraphs of (read pathInputFile)

Find and replace in text files using AppleScript

I am trying to write an applescript which will run via a launch agent. What the script needs to do is edit a user preference plist file so that default save locations are specific to that user. I am aware that this can be done by just setting "~/documents" as the location in the template plist. But Premier Pro for example also needs to write scratch files to a local drive. For simplicity I would like each user to have these put in a locations based on their username. This script will only need to run if the local profile has just been created from a template at first log on.
I have started by using some sample code found on this site and just making a simple test below. This test should edit a txt file and replace one word with another. This script is currently not working. When tested it opens up test.txt in TextEdit but does nothing more. No errors are displayed either.
Thank you in advance
John.
replaceText("replace this", "replace with this", "/Volumes/USB_Drive/test.txt")
on replaceText(search_string, replacement_text, this_document)
tell application "TextEdit"
open this_document
set AppleScript's text item delimiters to the search_string
set this_text to the text of the front document as list
set AppleScript's text item delimiters to the replacement_text
set the text of the front document to (this_text as string)
close this_document saving yes
end tell
end replaceText
Here an version that doesn't need text edit. It will read the file in utf-8 encoding, update it's contents and store that back into the file as utf-8 encoded text. The reason I use an try block around writing the file is that there will be an error if another application has the file open with read permission at the same time. The considering case block can be wrapped around the set ti to every text item of theContent if you want the search and replace case sensitive. There is no need for this to be active when you replace the string, only for finding it.
set stringToFind to "replace that"
set stringToReplace to "with this"
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
Well, yes, as #dj_bazzie_wazzie points out, you really don't need a text editor for this, you can use the terminal and do something like:
perl -pi -e 's/old text/new text/g' /path/to/theFile.plist
which of course you can use in AppleScript with the powerful do shell script command:
do shell script "perl -pi -e 's/" & search_string & "/" & replacement_text & "/g' " & quoted form of (POSIX path of file_path)
--assuming file_path is a variable with Mac-style (colon-separated) file path.
Modified from http://discussions.apple.com/message/20542594#20542594, the handler below does what you want. I made a few changes and added your variables. A few notes: (1) the 'my' before the handler call makes sure it is seen as the script's handler and not something TextEdit should interpret 'internally' (because it is in a tell block); (2) 'considering case' makes the handler case sensitive, which I assume you want; (3) You might consider something like TextWrangler, which has robust and feature-rich AppleScript support, and includes text replacement in its AS dictionary, as does Smile, a fantastic Script Editor (which can work with text, and formats plist files nicely).
tell application "TextEdit"
set workingWin to open this_document
set this_text to the text of the workingWin
set the text of the workingWin to (my replaceText(this_text, search_string, replacement_text))
close workingWin saving yes
end tell
to replaceText(someText, oldItem, newItem)
(*
replace all occurances of oldItem with newItem
parameters - someText [text]: the text containing the item(s) to change
oldItem [text, list of text]: the item to be replaced
newItem [text]: the item to replace with
returns [text]: the text with the item(s) replaced
*)
considering case
set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, oldItem}
try
set {itemList, AppleScript's text item delimiters} to {text items of someText, newItem}
set {someText, AppleScript's text item delimiters} to {itemList as text, tempTID}
on error errorMessage number errorNumber -- oops
set AppleScript's text item delimiters to tempTID
error errorMessage number errorNumber -- pass it on
end try
end considering
return someText
end replaceText

Clicking checkbox on web page using Applescript

I'm somewhat new to Applescript, and I am trying to make Applescript check a checkbox to select it. I want the checkbox to be clicked regardless of whether or not it's already checked. Here is the checkbox's location according to the Accessibility Inspector:
<AXApplication: “Safari”>
<AXWindow: “Studio”>
<AXGroup>
<AXGroup>
<AXGroup>
<AXScrollArea: “”>
<AXWebArea: “”>
<AXGroup: “”>
<AXCheckBox: “”>
Attributes:
AXRole: “AXCheckBox”
AXSubrole: “(null)”
AXRoleDescription: “check box”
AXChildren: “<array of size 0>”
AXHelp: “”
AXParent: “<AXGroup: “”>”
AXPosition: “x=1104 y=825”
AXSize: “w=18 h=19”
AXTitle: “”
AXDescription: “”
AXValue: “0”
AXFocused (W): “0”
AXEnabled: “1”
AXWindow: “<AXWindow: “Studio”>”
AXSelectedTextMarkerRange (W): “<AXTextMarkerRange 0x101937860 [0x7fff76e43fa0]>{startMarker:<AXTextMarker 0x1019378b0 [0x7fff76e43fa0]>{length = 24, bytes = 0xac01000000000000c0366e23010000001700000001000000} endMarker:<AXTextMarker 0x101938030 [0x7fff76e43fa0]>{length = 24, bytes = 0xac01000000000000c0366e23010000001700000001000000}}”
AXStartTextMarker: “<AXTextMarker 0x101938030 [0x7fff76e43fa0]>{length = 24, bytes = 0xa00000000000000098975e0d010000000000000001000000}”
AXEndTextMarker: “<AXTextMarker 0x1019378b0 [0x7fff76e43fa0]>{length = 24, bytes = 0xa200000000000000405e7812010000000000000001000000}”
AXVisited: “0”
AXLinkedUIElements: “(null)”
AXSelected: “0”
AXBlockQuoteLevel: “0”
AXTopLevelUIElement: “<AXWindow: “Studio”>”
AXTitleUIElement: “(null)”
AXAccessKey: “(null)”
AXRequired: “0”
AXInvalid: “false”
AXARIABusy: “0”
Actions:
AXPress - press
AXShowMenu - show menu
I've tried multiple methods to get this to work, and I haven't been able to. Any help is appreciated.
Your question with the Accessibility Inspector info is not very helpful I am afraid.
It would help if we could see the actual elements of the web page,
Have a look at this page which I found that shows check boxes and the code that makes it up.
Each element has a name and maybe within some other element.
on the page I can use this Applescript/Javascript to check the check1 checkbox.
Hopefully this will give you an idea of how to go about it.
But remember this code snippet is tailored to this page.
Open the web page and run this applescript
tell application "Safari"
set doc to document 1
do JavaScript "document.forms['testform']['check1'].checked = true" in doc
end tell
Update: Applescript GUI
Update:2 take into account "clicked regardless of whether or not it's already checked"
Taking a punt with your Accessibility Inspector. Which is a bit useless (not your fault)
try:
activate application "Safari"
tell application "System Events"
set theCheckbox to (checkbox 1 of group 3 of UI element 1 of scroll area 1 of group 1 of group 1 of group 2 of window 1 of application process "Safari")
set isEnabled to value of theCheckbox as boolean
if not isEnabled then
click theCheckbox
end if
end tell

how to select a file by default when open a folder in vfp 9.0

I want to open a folder, and select a file by default.
I do it like this:
Declare Long WinExec In kernel32 String #, Integer
WinExec("Explorer /select, C:\tt.txt",5)
But if the folder has been opened, the file can't be selected by default.
How to do it?
What is your purpose of prompting a user with picking a particular file...
The closest you can get from wthin VFP is "GetFile()" where you can give it a default extension of a file you are hoping to find and it brings up a file selection dialog.
lcFileSelected = GetFile( "Txt", "Caption left of combobox selection (but only shows about 16 chars)", "Button Caption", nOptionalButton )
where ex:
nOptionalButton
0 = no extra button at bottom right, just the OK, Cancel (where OK is overridden by the "Button Caption" sample above.
1 = OK, New, Cancel
2 = Ok, None, Cancel
If a value selected, you'll have the file name, otherwise blank.
REVISED ANSWER..
Then what you want is PUTFILE() which allows you to prompt a user a simple message, similar to a "Save to", and allows to put a fully qualified path and file name. Upon return, much like that of doing GETFILE() will return the final path/file name entered by the user. Ex:
lcUserAnswer = PUTFILE( "save where", "C:\program files\myTest.txt" )
now you can do whatever with the "lcUserAnswer" variable...

Resources