How's everyone doing?
So, if I run xdotool getactivewindow getwindowname, it gives the full title of the current window, for example:
fish home/kibe/Documents — Konsole
Blablablabla - Stack Overflow - Google Chrome
The thing is, I only want the application name (Konsole and Google Chrome).
I can easily do it in Python, as such:
def getAppTitle (fullStr):
lastDashIndex = None
for i in range(len(fullStr)):
if fullStr[i] == '-' or fullStr[i] == '—':
lastDashIndex = i
return fullStr[lastDashIndex+2:] if lastDashIndex else fullStr
print(getAppTitle('blablabla - blablabla - ApplicationName'))
# returns ApplicationName
I have been trying to do the same in shell script but I can't do it for the life of me. Also, for some reasons, some applications use "-" (normal dash) and others "—" (em dash).
How can I do that in shell?
You have to use this 'em dash' or dash as the field separator and print the last field:
xdotool getactivewindow getwindowname | awk -F"—|-" '{print $NF}'
I am not sure where this 'em dash' comes from, I had to copy paste it for the above command.
Maybe better, use two characters as the FS, any dash and a space, to get the same as your script, with the space trimmed.
xdotool getactivewindow getwindowname | awk -F"— |- " '{print $NF}'
Related
I am writing a shell script, in which I get the location of java via which java. As response I get (for example)
/usr/pi/java7_32/jre/bin/java.
I need the path to be cut so it ends with /jre/, more specificly
/usr/pi/java7_32/jre/
as the programm this information is provided to can not handle the longe path to work.
I have used cut with the / as delimiter and as I thought that the directory of the Java installation is always the same, therfore a
cut -d'/' -f1-5
worked just fine to get this result:
/usr/pi/java7_32/jre/
But as the java could be installed somewhere else aswell, for example at
/usr/java8_64/jre/
the statement would not work correctly.
I need tried sed, awk, cut and different combinations of them but found no answer I liked.
As the title says I would count the number of appereance of the car / until the substing jre/ is found under the premisse that the shell counts from the left to the right.
The incremented number would be the the field I want to see by cutting with the delimiter.
path=$(which java) # example: /usr/pi/java7_32/jre/bin/java
i=0
#while loop with a statment which would go through path
while substring != jre/ {
if (char = '/')
i++
}
#cut the path
path=$path | cut -d'/' -f 1-i
#/usr/pi/java7_32/jre result
Problem is the eventual difference in the path before and after
/java7_64/jre/, like */java*/jre/
I am open for any ideas and solutions, thanks a lot!
Greets
Jan
You can use the shell's built-in parameter operations to get what you need. (This will save the need to create other processes to extract the information you need).
jpath="$(which java)"
# jpath now /usr/pi/java7_32/jre/bin/java
echo ${jpath%jre*}jre
produces
/usr/pi/java7_32/jre
The same works for
jpath=/usr/java8_64/jre/
The % indicates remove from the right side of the string the matching shell reg-ex pattern. Then we just put back jre to have your required path.
You can overwrite the value from which java
jpath=${jpath%jre*}jre
IHTH
You can get the results with grep:
path=$(echo $path | grep -o ".*/jre/")
Ok so bear with me as I am not a professional, this is a proof of concept project to learn more about my shell, programming and just basic bash scripting.
So WHAT I WANT TO DO is: whenever anything is printed out in my terminal, be it the result of a command or an error message from the shell I want to apply some "filters" to what is being displayed so for example if I input "ls -a" in the terminal I would like to get the list of folders that the command returns but apply a TIME DELAY to the characters so that it seems like the list is being typed in real time.
More SPECIFICALLY I'd like for the script to take every alphanumerical character in STDOUT and spend a specific amount of time (say 100 milliseconds) iterating through random characters (these can be accessed randomly from a list) before finally stopping at the original value of the character.
WHAT I KNOW:
not much, I am new to programming in general so also the bash language but I can read some code and browsing through I found this http://brettterpstra.com/2012/09/15/matrixish-a-bash-script-with-no-practical-application/ script that plays with tput. This shows me the visual effect I'd like to accomplish can be accomplished...now to make it happen orderly and individually for each character printed to STDOUT...that is what I can't figure out.
WHAT I THINK:
in my mind I know I could take the STDOUT and pipe it to a file in which through any language (let's say python!) I can do all kinds of string manipulation and then return the output to STDOUT but I'd like for the characters to be manipulated in realtime so if for example the code was
cool_chars="£ ア イ ウ エ オ カ キ ク ケ コ サ シ ス "
stdout=whatever module works to grab STDOUT from shell as string
stdout = stdout.split(" ")
for word in stdout:
for letter in word:
n=0
while (n<10):
#print the following iteration in real time # shell but how????
print random.choice(cool_chars)
#finally stop at correct character
print letter
n++
Anyway, I've read a little about curses and ncurses and how you can create new windows with whatever specified parameters, I wonder if it'd be just a matter of creating a terminal with the specified parameters with the curses libraries and then making a link so that each new terminal instance opens my modified curses shell or if I can just do a bash shell script or if it'd be easiest to use something like python. I know all of the above can be options but I'm looking for the simplest, not necessarily most resource efficient answer.
Any help, comments, pointers etc is appreciated.
This does not answer you question fully, but it does print any input as if it was being type in real time:
perl -MTime::HiRes -F -ane '$|=1;$old=""; foreach $char(#F){Time::HiRes::sleep(0.1); print "\r${old}${char}"; $old.=$char}' /etc/hosts
instead of file, STDIN can be used:
echo -e "abc\ndef\nghi" | perl -MTime::HiRes -F -ane '$|=1;$old=""; foreach $char(#F){Time::HiRes::sleep(0.1); print "\r${old}${char}"; $old.=$char}'
We can make it shorter using shell's sleep:
perl -F -ane '$|=1;$old=""; foreach $char(#F){`sleep 0.1`; print "\r${old}${char}"; $old.=$char}'
EDIT:
The script below should fully solve your problem:
#!/usr/bin/perl
use strict;
use utf8;
binmode(STDOUT, ":utf8");
our $cols=`tput cols`;
our $|=1;
our $cursor="";
sub reset_line {
print "\r" . " "x$cols . "\r";
}
sub pick_cursor {
my #c = split (//,"£アイウエオカキクケコサシス");
$cursor=$c[int(rand(1+#c))];
}
while (<>) {
my $line="";
my #a=split //;
foreach my $char (#a) {
`sleep 0.1`;
reset_line;
pick_cursor;
if ( $char eq "\n" || $char =~ /\s/) {
print "${line}${char}";
}else {
print "${line}${char}${cursor}";
}
$line .= $char;
}
}
I am trying to print the paths that are located in a file .chsrc which is easy to print with just echo-ing $path, but I need to add something to the end of each path that is listed. Ie:
/opt/local/bin: /usr/ucb: /usr/bin:
I can not edit or change the .chsrc file. I also tried to find something on concatenating in C Shell, but that seems to not really "exist" in C Shell from what I read. I am sorry if I sound arrogant in anyway, I am new to C Shell. If anyone has any pointers, advice is always great! Thank you!
echo "$PATH" | sed 's/:/: /g;s/ *$//'
's/'=substitute, '/:/: /'=targetPattern/replacmentPattern, 'g'=do replacment globally (on the current line), ';'= command separator, 's/ *$//'= substitute any trailing spaces at the end-of-the line '$', replacementPattern='//'=(nothing, empty, nada, zilch;-)
Best to always echo any env var surrounded by dbl-quotes unless you want any spaces in the variable to cause word splitting in the commandline evaluatio. Especially with PATH, as space char is legal in a path.
In general, concatenation works like this in CSH
set var1 = text1
set var2 = myText
echo "someText "$var1 " more stuff"$var2
# -----------^^^^^^^^^^^^^^^^--- deliberate, copy paste as is
I don't have a csh to print the output with, but cut and paste these lines and you'll see that spaces outside of " .... ", get reduced to 1 space, spaces inside of "...." stay in place, as many as you want, AND variables, bumped up next to "text Strings" do NOT insert a space char automatically, you have to put them in.
I don't see any arrogance in this question ;-)
But... before you spend 8+ years of your life using a 2nd rate shell ( ;-( ), read everything at A great Unix Primer , especially Why csh is less than perfect scripting language ;-)
P.S. Welcome to StackOverflow (S.O.) Please remeber to read the FAQs, http://tinyurl.com/2vycnvr , vote for good Q/A by using the gray triangles, http://i.imgur.com/kygEP.png , and to accept the one answer that best solves your problem, if any, by pressing the checkmark sign , http://i.imgur.com/uqJeW.png
I need to prevent 750 Macs from updating Firefox, but retain individuals preferences.
Therefore I must insert a line into a file.
Using a shell script with sed, I am struggling with identifying the line to follow, as it has special characters:
sed '/user_pref("accessibility.typeaheadfind.flashbar", 0); /a\
user_pref("app.update.enabled", false);\'$'\n' /Users/username/pathToFile/prefs.js
Obviously for now this is printing to screen, but will write out to a file later on. Any help appreciated.
The source file looks like:
# Mozilla User Preferences
/* Do not edit this file.
*
* If you make changes to this file while the application is running,
* the changes will be overwritten when the application exits.
*
* To make a manual change to preferences, you can visit the URL about:config
* For more information, see http://www.mozilla.org/unix/customizing.html#prefs
*/
user_pref("accessibility.typeaheadfind.flashBar", 0);
user_pref("app.update.lastUpdateTime.addon-background-update-timer", 1298297884);
user_pref("app.update.lastUpdateTime.background-update-timer", 1298282703);
I need to insert the line of code pre-mentioned within the sed command as a new line after the first instance of the "user_pref", so the dollar and \n achieve this, if I'm right.
It looks like the code is right and it should work, but doesn't. I'm using the bash shell on OS 10.6.8.
OK, I fixed this myself. Stupid typo:
sed '/user_pref("accessibility.typeaheadfind.flashbar", 0);/ a\
Compare the above with the original posting and you will notice at the end that the forward slash needed to come right after the semi-colon.
For those interested, I actually settled upon:
useris=$(who | grep -v support | awk '{print $1}' | awk "NR==1{print;exit}")
firefoxPath=$(grep "Path=" /Users/"$useris"/Library/Application\ Support/Firefox/profiles.ini | awk -F '=' '{print $2}')
sed '/^user_pref("app.update.lastUpdateTime/ i\
user_pref("app.update.enabled", false);\'$'\n' /Users/"$useris"/Library/Application\ Support/Firefox/"$firefoxPath"/prefs.js > /Users/"$useris"/Library/Application\ Support/Firefox/"$firefoxPath"/prefs.txt
You then follow up with replacing the "prefs.js" file with the new one you generated and you have now managed to prevent the Firefox browser from prompting users with an update.
I'm trying to get the current display resolution of both of my displays depending on where the mouse cursor is.
i.e. when the mouse cursor is on the first display I want to get the resolution of this display.
With a shell script I can get both resolutions:
set screenWidth to (do shell script "system_profiler SPDisplaysDataType | grep Resolution | awk '{print $2}'")
But I don't get which display is currently "active".
Any ideas?
This does the trick:
tell application "Finder"
set screen_resolution to bounds of window of desktop
end tell
Applescript does not have any access to cursor location, even via System Events. Sorry.
[There are a couple commercial solutions, but I'm guessing they're not worth the trouble in this case? I suppose I could also whip up a quick command-line tool that just returns the current cursor location... worth the trouble?]
p.s. awk is great at finding matching lines:
set screenWidth to (do shell script "system_profiler SPDisplaysDataType | awk '/Resolution/{print $2}'")
For the sake of even more completeness, here is the code to get the width, height, and Retina scale of a specific display (main or built-in).
This is the code to get the resolution and Retina scale of the built-in display:
set {width, height, scale} to words of (do shell script "system_profiler SPDisplaysDataType | awk '/Built-In: Yes/{found=1} /Resolution/{width=$2; height=$4} /Retina/{scale=($2 == \"Yes\" ? 2 : 1)} /^ {8}[^ ]+/{if(found) {exit}; scale=1} END{printf \"%d %d %d\\n\", width, height, scale}'")
And this is the code to get the resolution and Retina scale of the main display:
set {width, height, scale} to words of (do shell script "system_profiler SPDisplaysDataType | awk '/Main Display: Yes/{found=1} /Resolution/{width=$2; height=$4} /Retina/{scale=($2 == \"Yes\" ? 2 : 1)} /^ {8}[^ ]+/{if(found) {exit}; scale=1} END{printf \"%d %d %d\\n\", width, height, scale}'")
The code is based on this post by Jessi Baughman and the other answers given here.
The following does not solve the OP's problem, but may be helpful to those wanting to determine the resolution of ALL attached displays in AppleScript (thanks to #JoelReid and #iloveitaly for the building blocks):
set resolutions to {}
repeat with p in paragraphs of ¬
(do shell script "system_profiler SPDisplaysDataType | awk '/Resolution:/{ printf \"%s %s\\n\", $2, $4 }'")
set resolutions to resolutions & {{word 1 of p as number, word 2 of p as number}}
end repeat
# `resolutions` now contains a list of size lists;
# e.g., with 2 displays, something like {{2560, 1440}, {1920, 1200}}
For the sake of completeness, here is the code to grab the screen height:
do shell script "system_profiler SPDisplaysDataType | awk '/Resolution/{print $4}'"}
Multi-Monitor and Retina detection
To get the width, height and scaling (retina = 2, else = 1) for all monitors:
set resolutions to {}
repeat with p in paragraphs of ¬
(do shell script "system_profiler SPDisplaysDataType | awk '/Resolution:/{ printf \"%s %s %s\\n\", $2, $4, ($5 == \"Retina\" ? 2 : 1) }'")
set resolutions to resolutions & {{word 1 of p as number, word 2 of p as number, word 3 of p as number}}
end repeat
get resolutions
Based on answers above.
Results in something like this:
{{2304, 1440, 2}, {1920, 1080, 1}}
On my machine system_profiler takes nearly a second to return a reply. For my purposes, that way too long.
Pre-10.12, I used ASObjC Runner but apparently that no longer works.
This is much faster for me:
tell application "Finder" to get bounds of window of desktop
(Taken from https://superuser.com/a/735330/64606)
I have a shell script that makes use of cliclick and displayplacer, both available in Homebrew: https://github.com/huyz/trustytools/blob/master/mac/get-bounds-of-mouse-display.sh
To use from within AppleScript:
set displayBounds to do shell script "PATH=/opt/homebrew/bin:$PATH /Users/huyz/bin/get-bounds-of-mouse-display | xargs -n 1 echo"
set displayBounds to the paragraphs of displayBounds