I was just wondering how someone would go about finding all the applications that are installed on Mac OS X 10.5 using preferably applescript and output all their application names to a text file.
All the applications installed under Mac OS X are registered in the Launch Services database.
The Launch Services framework contains a helper shell command lsregister which among other uses can dump the information stored in the Launch Services database. Under Mac OS X 10.5 and 10.6 that command is located in the following folder:
/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister
Using a few simple grep filters one can extract the full paths of all registered applications:
lsregister -dump | grep --after-context 1 "^bundle" | grep --only-matching "/.*\.app"
Putting it all together, the following AppleScript will compute the user visible names of all registered applications using the info for command:
property pLSRegisterPath : "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister"
set theAppPaths to every paragraph of (do shell script pLSRegisterPath & " -dump | grep --after-context 1 \"^bundle\" | grep --only-matching \"/.*\\.app\"")
set theNames to {}
repeat with thePath in theAppPaths
try
copy displayed name of (info for (thePath as POSIX file)) to end of theNames
end try
end repeat
choose from list theNames
There are a few methods in this post, depending on how in-depth you want your search to be. Also not sure if that's exactly the output format you want, but you can probably tweak it for your specific needs.
I use the system_profiler command to get my text. Then you can parse as needed.
system_profiler SPApplicationsDataType
...
AppleScript Utility:
Version: 1.1.1
Last Modified: 5/18/09 10:34 PM
Kind: Intel
64-Bit (Intel): Yes
Location: /System/Library/CoreServices/AppleScript Utility.app
maybe pipe it to a text file and then use sed ....
Bash commands can be called through applescript if you want to have an application or you can save the script with a .command extension and the user will be able to double-click on it.
For poeple like me using bash scripting to reach their goals here is a bash variant of the script:
#!/usr/bin/env bash
path='/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support'
$path/lsregister -dump | grep -A 1 "^bundle" | grep --only-matching "/.*\.app" | awk -F "/" '{ print $NF }' | awk -F "." '{ print $1 }'
This gives a list of all apps without the .app extension.
Related
I'm using this BetterTouchToll for make my touch bar more interesting, what is very cool.
He accept some Apple Scripts for more dynamic, so I start to study this scripts.
Now I wanna to display my Magic Mouse Battery on my touch bar, for this I was trying this code, but is not working.
if application "Mouse" is running then
tell application "Mouse"
return (get Battery)
end tell
end if
return "no mouse"
My guess is that Mouse is not a application, but don't know what to put in the place
The traditional means of getting the battery level is to use ioreg on the command line. However, the traditional means of doing this no longer seem to work as of at least macOS High Sierra/10.13.4; that is, they no longer allow choosing to display just the battery percentage of a single bluetooth device.
So this is a hack that assumes that the Magic Mouse is always the last device displayed by ioreg. This is likely to fail, if not across different installations of macOS, then across different versions.
ioreg -c AppleDeviceManagementHIDEventService | grep BatteryPercent | tail -1 | sed 's/[^[:digit:]]//g'
In an AppleScript, this would be:
do shell script "ioreg -c AppleDeviceManagementHIDEventService | grep BatteryPercent | tail -1 | sed 's/[^[:digit:]]//g'"
You have your code setup to also detect when the Magic Mouse is not connected. The product name is in the property “Product” in ioreg. For example:
ioreg -c AppleDeviceManagementHIDEventService | grep '"Product" ='
So to make sure that this final device is the Mouse, you could do:
set finalDevice to do shell script "ioreg -c AppleDeviceManagementHIDEventService | grep '\"Product\" =' | tail -1"
if finalDevice contains "Magic Mouse" then
set remaining to do shell script "ioreg -c AppleDeviceManagementHIDEventService | grep BatteryPercent | tail -1 | sed 's/[^[:digit:]]//g'"
remaining & "%"
else
"no mouse"
end if
The basic logic:
Grab the list of all products using ioreg.
Use tail to get only the final product in the list.
If the final product is a Magic Mouse, then:
Grab the list of all battery percentages using ioreg.
Use tail to get only the final battery percentage in the list.
Use sed to get only the actual number from that line.
Append a percentage symbol to the number.
Otherwise, there is no mouse. (Or the mouse is not the final item in the list.)
For the older means of using ioreg, see, for example:
Reporting on Bluetooth Mouse/Keyboard battery status
Read Magic Mouse and Apple Wireless Keyboard Battery percentage
Thanks so much for the solution. Very inspiring. As I have multiple devices I made my own script based on your solutions. I want to share it here in case it could be useful to others.
As I have more than one bluetooth device, the order of them in ioreg is based on the order they were connected. Which means I cannot assume that the mouse is the last device.
I made most of it in shell, not in applescript, since I am more experienced in shell and therefore it was quicker that way. Using Applescript for filtering the output from ioreg would properly have been a 'cleaner' solution :p
WARNING: I know this code is pretty crappy, but it was quick to write and it does the job, don't assume this to be a way of doing things properly.
My solution
From BTT the following script calls the shell script
set devicename to "Magic Mouse 2" -- The name of the device in ioreg
set displayname to "Mouse" -- The name to display on the touchbar
set remaining to do shell script "~/.dotfiles/shell/device_battery_level.sh" & " " & quoted form of devicename
if remaining is "" then
"" --No device present = no output to touchbar
else
displayname & " " & remaining & "%" -- Show output on touchbar
end if
As seen the code pretty much just calls the shell script. The name provided in the variable "devicename" is used as a argument to the script and is the name that the script will look for in ioreg. If the shell script outputs an empty script no widget will be displayed. For me this was preferred over displaying "No device".
The script in "~/.dotfiles/shell/device_battery_level.sh" then looks like this:
#!/bin/sh
DEVICES=$(ioreg -r -l -n AppleHSBluetoothDevice | egrep '"BatteryPercent" = |^ \| "Bluetooth Product Name" = ') #Lets get a list of all bluetooth devices
DEVICELINE=$(grep -n "$1" <<< "$DEVICES") #$1 is the device that this script was called with. Lets extract only the line with that device
if [$DEVICELINE = ""] #If DEVICELINE is empty the name of the device was not in the output and it is properly not connected.
then
echo "" #Device not present, lets give BTT an empty string
else
LINENR="${DEVICELINE:0:1}" #Then we find out where the line of the device is located
NEXTLINE=$(expr $LINENR + "1") #The battery level is at the next line therefore we increment the line number
SEDCOMMAND="p"
BATTERYLINE=$(echo "$DEVICES" | sed -n $NEXTLINE$SEDCOMMAND) # Now we can extract the line with the battery percent
echo $BATTERYLINE | sed 's/[^[:digit:]]//g' #Finally we just need to get the digit and echo that to BTT
fi
The basic logic is the same as the above answer. Except instead of grabbing the last line from ioreg with tail egrep is used to only output relevant lines. The egrep code is based on another post here: https://apple.stackexchange.com/questions/293502/how-can-i-determine-the-battery-level-of-my-magic-mouse-from-the-command-line/293505#293505
Based on this the line which mentions the devicename is found. The logic is that since the battery level information is always below the devicename in ioreg the next line in the extracted list must be the battery level.
Similar question to How to keep from duplicating path variable in csh and How to keep from duplicating path variable in ksh. But this time using Windows command line (cmd.exe).
PATH=C:\path\to\bin;%PATH%
How do I remove duplicates from PATH? It would like to do this in an automated way: as part of our build process a specific path is added to the environment variable PATH; when done 20+ times that path is present 20+ times. I want to avoid that.
My solution is similar to https://stackoverflow.com/a/586748/911550:
echo %PATH% | tr ; \n | awk "!($0 in a) { a[$0]; print }" | paste -sd; - > TEMPORARY.FILE
set /P PATH=< TEMPORARY.FILE
del TEMPORARY.FILE
tr, awk and paste can be downloaded from http://gnuwin32.sourceforge.net. (Actually, the first I do on any new Windows workstation is using the Automated gnuwin32 download tool)
I am currently looking to use "diskutil cs list" to show the logical volume groups. I need to be able to isolate the UDID.
Example line from "diskutil cs list"
+-- Logical Volume Group B848BCC7-6FFA-4643-AFE1-56FCA333A6B5
Previously my though process was to;
diskutil cs list | grep 'Group'
I think AWK will be a better route to show just the string of letters and numbers. I have been unsuccessful in finding out how to dow so
Ultimately, i will use the UDID in a shell script for reformatting a Fusion drive. Using something similar to below.
set a to (do shell script "diskutil cs list|grep 'Group'")
I'd like it to set a to the UDID and not the full line.
Try:
diskutil cs list | awk '/Group/{print $NF}'
/Group/ will look for lines that have the word Group in them. It is just a filter mechanism. If all your output lines have Group in them, then you can remove /Group/ part.
Once it finds those lines, it will print the last element of that line. awk by default splits the line on space.
Depending how new your system is you might have lsblk(1) command, that is made to be combined with scripts. The utility can limit what block devices are listed, and what information is displayed in format that can be defined.
diskutil cs list | grep -Po 'Group \K\S*$'
So I am on an old (fully aluminum) macbook pro running snow leopard and using terminal to ssh into remote hosts on my work network. I am noticing a strange thing when I copy and paste things in the terminal.
For example I will grep for something like this in a file:
samtools view sorted-616.bam | grep 'SOLEXA9:1:1:30:3316:10211' | head -n 1
and it gives
SOLEXA9:1:1:30:3316:10211 69 k26_179705 159 0 * = 159 0 TATGCCGCCAAACGCTTCCGCAAAGCTCTGTGTTTGACTATGTAGCGACTA CBCCCCCC#CCCCCCCCC?#CC?CC########################## RG:Z:1
But now when I select it, hit command+c to copy, and then command+v to paste, it comes out like this:
SOLEXA9:1:1:30:3316:1021169k26_1797051590*=1590TATGCCGCCAAACGCTTCCGCAAAGCTCTGTGTTTGACTATGTAGCGACTACBCCCCCC#CCCCCCCCC?#CC?CC##########################RG:Z:1
Notice how there are no spaces in between fields now. Is there a special method to copy and paste things exactly as they are?? Why is terminal behaving this way?
What happens when you use pbcopy?
samtools view sorted-616.bam | grep 'SOLEXA9:1:1:30:3316:10211' | head -n 1 | pbcopy
This should do the same thing that the copy command does, but without having to select the output you want.
Have you tried another terminal emulator? I use iTerm2 because it will copy to the clipboard on selection without having to hit Command-c.
EDIT: You may have to install Apple's developer tools to get the pbcopy/pbpaste tools.
No idea why the spaces are missing from the pasted text, but I'd try to write the output to a file, open up the file in an editor and try to see if it's something other than the standard space/newline characters. You seem to know your way with piping, but anyway:
samtools view sorted-616.bam | grep 'SOLEXA9:1:1:30:3316:10211' | head -n 1 > file.txt
It might depend on which system the host is running. I have had some issues while on ssh connections to linux/unix hosts, while Mac-to-Mac usually works just fine.
I am trying to create something in Perl that is basically like the Unix tee command. I'm trying to read each line of STDIN, run a substitution on it, and print it. (And eventually, also print it to a file.) This works if I'm using console input, but if I try to pipe input to the command it doesn't do anything. Here's a simple example:
print "about to loop\n";
while(<STDIN>)
{
s/2010/2009/;
print;
}
print "done!\n";
I try to pipe the dir command to it like this:
C:\perltest>dir | mytee.pl
about to loop
done!
Why is it not seeing the piped input? (I'm using Perl 5.10.0 on WinXP, if that is relevant.)
This is actually a bug in how Windows handles IO redirection. I am looking for the reference right now, but it is that bug that requires you to specify
dir | perl filter.pl
rather than being able to use
dir | filter
See Microsoft KB article STDIN/STDOUT Redirection May Not Work If Started from a File Association:
Start Registry Editor.
Locate and then click the following key in the registry:
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer
On the Edit menu, click Add Value, and then add the following registry value:
Value name: InheritConsoleHandles
Data type: REG_DWORD
Radix: Decimal
Value data: 1
Quit Registry Editor.
C:\Temp> cat filter.pl
#!/usr/bin/perl
while ( <> ) {
print "piped: $_";
}
C:\Temp> dir | filter
piped: Volume in drive C is MAIN
piped: Volume Serial Number is XXXX-XXXX
piped:
piped: Directory of C:\Temp>
piped:
piped: 2010/03/19 03:48 PM .
piped: 2010/03/19 03:48 PM ..
piped: 2010/03/19 03:33 PM 32 m.pm
piped: 2010/03/19 03:48 PM 62 filter.pl
Try:
C:\perltest>dir | perl mytee.pl
Could it be Microsoft KB #321788?
Scripts that contain standard input
(STDIN) and standard output (STDOUT)
may not work correctly if you start
the program from a command prompt and
you use a file association to start
the script.
There's nothing wrong with trying to learn by doing, but a quick search of CPAN shows a number of possible solutions for the tee in Perl problem.
For example: PerlIO::Tee.
Well IMHO, perl is poor substitute for sed ;)
dir | sed s/2009/2010/