I am trying to create an apple script that alerts me every time a key word is returned in terminal (I have a program that runs in terminal and returns a lot of data). In my case I use iTerm instead of terminal, but it's the same idea.
So if the word "any" is returned in iTerm, I would like the result to be "found any".
The code below is returning Result: {"found %s", {"any", "of", "these", "words"}}
application "iTerm"
set found to ["any", "of", "these", "words"]
if the found exists then
return {"found %s", found}
end if
Related
There was a problem when im creating the CLI. I'm want to give the user the opportunity to insert their data into a text file, for this I created a file and added a heredoc to it
I'm trying to get data from a text document that has a heredoc inside of it with a function that is supposed to interpolate
When I try to display the result of the file, I get the entire contents of the file, including the heredoc
an example will be below
I tried to solve my problem through File class
variable_name = File::open("path_directory/file_with_heredoc.txt", "r+")::read
Next, I decided to give the value of the variable to the terminal via
exec("echo #{variable_name}")
The terminal displays
file = <<-EOM
single text with def result: #{upcase_def("Hello")}
EOM
Tried to give through struct, but result is unchanged
exec("echo #{variable_name.strip}")
What do I need to do to get only data, no HEREDOC syntax?
I want to get this result
"single text with def result: HELLO"
I think this is what you are trying to do but I recommend you to first do some research why 'eval() is evil'. If the file is a user (or hacker) input you definitely want some sanitization there or a completely different approach.
def upcase_def(str)
str.upcase
end
data = File.read('file_with_heredoc.txt')
eval(data)
# => " single text with def result: HELLO\n"
Here is the case;
There is this app called "termux" on android which allows me to use a terminal on android, and one of the addons are androids API's like sensors, tts engines, etc.
I wanted to make a script in ruby using this app, specifically this api, but there is a catch:
The script:
require('json')
JSON.parse(%x'termux-sensor -s "BMI160 Gyro" -n 1')
-s = Name or partially the name of the sensor
-n = Count of times the command will run
returns me:
{
"BMI160 Gyroscope" => {
"values" => [
-0.03...,
0.00...,
1.54...
]
}
}
I didn't copied and pasted the values, but that's not the point, the point is that this command takes almost a full second the load, but there is a way to "make it faster"
If I use the argument "-d" and not use "-n", I can specify the time in milliseconds to delay between data being sent in STDOUT, it also takes a full second to load, but when it loads, the delay works like charm
And since I didn't specify a 'n' number of times, it never stops, and there is the problem
How can I retrieve the data continuously in ruby??
I thought about using another thread so it won't stop my program, but how can I tell ruby to return the last X lines of the STDOUT from a command that hasn't and will not ever stop since "%x'command'" in ruby waits for a return?
If I understood you need to connect to stdout from a long running process.
see if this works for your scenario using IO.popen:
# by running this program
# and open another terminal
# and start writing some data into data.txt
# you will see it appearing in this program output
# $ date >> data.txt
io_obj = IO.popen('tail -f ./data.txt')
while !io_obj.eof?
puts io_obj.readline
end
I found out a built in module that saved me called PTY and the spawn#method plus thread management helped me to keep a variable updated with the command values each time the command outputted new bytes
Using Xcode, I'm trying to pipe the output of a shell command that I use to check the list of available printers on a print server. It works in Terminal, but the output can only be seen in the debugger, and when I put the script into Script Editor, I get the output displayed as a "Syntax Error".
I'm trying to have it come up as an alert with the Display alert command but I've had no luck.
on Button_(sender)
set Printerlookup to "/usr/local/bin/iprntcmd --listprintersonserver printerserver.com"
set Printers to do shell script Printerlookup
display alert "The available printers are:" & Printers
end Button_
EDIT:
This is the whole appdelegate.
script AppDelegate
property parent : class "NSObject"
property txtBox: ""
-- IBOutlets
property theWindow : missing value
on applicationWillFinishLaunching_(aNotification)
-- Insert code here to initialize your application before any files are opened
end applicationWillFinishLaunching_
on applicationShouldTerminate_(sender)
-- Insert code here to do any housekeeping before your application quits
return current application's NSTerminateNow
end applicationShouldTerminate_
use scripting additions
on Button_(sender)
if txtBox is "" then
display alert "Please enter some text"
else
set iPrntlookup to "/usr/local/bin/iprntcmd --listprintersonserver "& txtBox &"printerserver.com"
set Printers to do shell script Printerlookup
display alert "The available printers are:" & Printers as text
end if
end Button_
end script
This is the expected output, which is found in the debugger area as opposed to the dialog box.
2018-05-07 21:34:12.925412+1000 iPrint Local[29523:430596] MessageTracer:
Falling back to default whitelist
2018-05-07 21:34:17.104407+1000 iPrint Local[29523:430596] *** -[AppDelegate
Button:]: iprntcmd v06.07.01
Listing printers on testprintserver.com.
ipp://testprintserver.com/ipp/printer1
ipp://testprintserver.com/ipp/printer2 (error 1)
Add a use scripting additions statement at the top of your script.
new to applescript and this small check is driving me nuts.
I want to check if the input to the script has the substring of "mob".
If i create a varible with mob1234, It works and returns true.
on run {input, parameters}
set testString to "mob1234"
display dialog {"MOB" is in testString}
return input
end run
If i change it to use the input, and set the input to mob1234, it fails and gives me false
on run {input, parameters}
set testString to input
display dialog {"MOB" is in testString}
return input
end run
I have no idea.
You don't say how you're invoking your script. If you're calling the script via Automator, be aware that in this mode input is a list, not a string, so this should work:
set testString to item 1 of input
Since the answer given by iayork seems to be right, here some follow up:
-- When this is saved as compiled script one can call it from Terminal like:
-- `osascript scriptName.scpt MOBstringArg1 arg2`
on run {input, parameters}
if (input's class is list) then set input to (item 1 of input) as text
if "MOB" is in input then
display notification "FOUND MOB" with title "In: " & input
end if
return input
end run
See comment from iayork about calling a script with osascript.
You can also just coerce the input to text directly when you assign it to testString: set testString to input as text, that is, if you aren't going to use any other list item of input.
The Problem
I want to press a key when I have a line highlighted and convert from a single line:
JGLogEntry *logEntry = [JGLogEntry applicationNoWindowsFrom:date1 to:date2 intoMOC:mockRawMOC];
to a multiline statement:
JGLogEntry *logEntry = [JGLogEntry applicationNoWindowsFrom:date1
to:date2
intoMOC:mockRawMOC];
What I've Tried
I've got a simple ruby script that almost gets me there.
#!/usr/bin/env ruby
s = STDIN.read
s.gsub!(/(:.+?\w) (\w.+?)/,'\1' + "\n\t" +'\2')
print s
When I set the output to "Replace Selection", I get this:
JGLogEntry *logEntry = [JGLogEntry applicationNoWindowsFrom:date1
to:date2
intoMOC:mockRawMOC];
When I set the output to "Place on Clipboard", then paste it in, I get the desired result:
JGLogEntry *logEntry = [JGLogEntry applicationNoWindowsFrom:date1
to:date2
intoMOC:mockRawMOC];
However, this is two keypresses which is clumsy.
Any ideas how I can get the replaced text to obey Xcode's auto indent rules?
Check the pre-installed script for "Convert tabs to spaces", and how it executes an in-line applescript. Use that to tell XCode to perform the menu item
Edit > Format > Re-Indent
I'm not sure how you do that with ruby, nor the details about the applescript content, but I would wager it's fairly straight-forward..