Hardware Temperature report - macos

I am trying to find a way to report the hardware temperatures back to me without a program that I have to download, I know of plenty that do that show but don't report back to a remote location on the network.
I found an applescript that I might be able to modify to get it to work but I am currently stuck at step 1 right now, getting the script to run.
Below is the script that I found. I keep getting an error at the marked line, lucky line 13.
The error just says can't get item 17.
set the_result to (do shell script "ioreg -c IOHWSensor | grep -vE '\\{|\\}|\\+\\-o'")'s paragraphs
set all_display to ""
repeat with i from 0 to 16
set jump to 3
set the_location to item (3 + (jump * i)) of the_result
set the_location to characters 41 thru ((count of characters of the_location) - 1) of the_location as string
set the_type to item (4 + (jump * i)) of the_result
set the_text to item (2 + (jump * i)) of the_result as string
**set the_text to characters 44 thru (count of characters of the_text) of the_text as string --(length of item 2 of the_result)**
set the_type to characters 37 thru ((count of characters of the_type) - 1) of the_type as string
if the_type = "temperature" then
set all_display to all_display & "
" & the_location & ": " & ((the_text / 65536) * (9 / 5)) + 32 & " F" as string
end if
end repeat
display dialog all_display
I was playing around with this again and finally able to get the complete error
Error: Can't make characters 44 thru 41 of " | | | | | \"version\" = 2" into type string.

I believe this information is reported by the IOHWSensor via ioreg. This one-liner works for me:
echo $((`ioreg -c IOHWSensor | grep "current-value" | grep -oE [0-9]+` / 65536))
Division by 65536 may be hardware-dependent. Some macs store the value as a multiple of 10, other powers of 2, etc. After dividing by the correct number, I believe the result is always in ºC.
Further reading:
http://www.cminow.org/ioreg_perl.html

Scrip searches for word "temperature" in the output of "ioreg -c IOHWSensor" command - but if you execute that from command line and grep for word "temperature" there will be none. At least I don't see it on my system.
Mine is OsX 10.7 what you have got there ?

Related

Detect battery percentage with applescript

How would I detect what my computer's battery percentage is at and set it to a variable using applescript? All answers to similar questions say to install additional software, but I would like to do this with purely applescript. Is this possible? I've also tried searching through the applescript library with no success.
on run
set theBattString to (do shell script "pmset -g batt")
-- the above will return something like...
-- Now drawing from 'Battery Power' -InternalBattery-0 82%; discharging; 4:06 remaining
end run
Now you can parse theBattString to get the information you'd like from it.
Another option...
on run
do shell script "ioreg -l | grep -i capacity | tr '\\n' ' | ' | awk '{printf(\"%.2f%%\\n\", $10/$5 * 100)}'"
-- will output...
-- 79.63%
end run
The applescript unsigned variable can be used like a signed FileMaker variable... the ** can be removed I tried it Bold it
set batteryPercent to do shell script "pmset -g batt "
tell application "FileMaker Pro.app"
tell table "Power Test Results"
tell record 1
set cell "Test percentage via AppleScript Global" to batteryPercent
end tell
end tell
end tell
Thank ThrowBackDewd for his answer.
I made something a bit strange but it works for me.
Here is the AppleScript
[EDIT]
Here a more efficient code thanks to user #user3439894.
set batteryPercent to word 6 of paragraph 2 of (do shell script "pmset -g batt")
if batteryPercent < 40 then
beep
repeat 3 times
say "Attention " & batteryPercent & "%" & " before shut down."
display notification "Attention " & batteryPercent & "%" & " of charge before shut down." sound name "Glass"
end repeat
end if
idle application
on idle
set batteryPercent to word 6 of paragraph 2 of (do shell script "pmset -g batt")
if batteryPercent < 40 then
repeat 3 times
beep
say "Attention " & batteryPercent & "%" & " of charge before shut down."
display notification "Attention " & batteryPercent & "%" & " of charge before shut down." sound name "Glass"
end repeat
end if
return 60
end idle
[First post]
set theBattString to (do shell script "pmset -g batt")
-- the above will return something like...
-- Now drawing from 'Battery Power' -InternalBattery-0 82%; discharging; 4:06 remaining
set batteryLevel to splitText(theBattString, space)
--set totalItembatLvl to length of batteryLevel
set remainingTime to item 9 of batteryLevel
if remainingTime < "2:40" then
display alert "low battery " & remainingTime & " before shut down."
--batteryLevel & " " & remainingTime
end if
on splitText(theText, theDelimiter)
set AppleScript's text item delimiters to theDelimiter
set theTextItems to every text item of theText
set AppleScript's text item delimiters to ""
return theTextItems
end splitText
on getPositionOfItemInList(theItem, theList)
repeat with a from 1 to count of theList
if item a of theList is theItem then return a
end repeat
return 0
end getPositionOfItemInList
You can add it in an idle statement and check every minute for your battery level.
Any suggestions or corrections will be appreciated.
Regards.

Executing two commands in VBscript

I'm trying to write a VBScript to execute a two commands. The script is as follows:
Set objShell = CreateObject("WScript.Shell")
Set X = objShell.Exec("opcdeploy -cmd ""dbspicao -m 0703 -r 1"" -node fssflx24.fss.india")
Set Y = objShell.Exec("opcmsg a=a o=o msg_text=X severity=Normal node=fssflx24.fss.india")
strIpConfig = objScriptExec.StdOut.ReadAll
WScript.Echo strIpConfig
What I want is when the first command "X" gets executed, it's output shall used as the msg_text in the second "Y" command.
But it is not happening as when the second command gets executed it captures not the output but only word "Y".
What I'm missing.
Kindly assist.
BR,
Ramesh
Well there are a couple of problems.
First X is set to the Execobject not the output of the exec.
Second in the Y Exec you have just a string. To use a variable in a string you have to end the string after msg_text= with a " and then append the variable X and append finally the rest of the string. So the code would look like:
Set objShell = CreateObject("WScript.Shell")
Set execo = objShell.Exec("opcdeploy -cmd ""dbspicao -m 0703 -r 1"" -node fssflx24.fss.india")
X = execo.StdOut.ReadAll
Set Y = objShell.Exec("opcmsg a=a o=o msg_text=" & X &" severity=Normal node=fssflx24.fss.india")
Finally I cannot tell from your code what objScriptExec is supposed to be. It seems to be defined somewhere else so I don't know for sure how it is related...
My first command
opcdeploy -cmd ""dbspicao -m 0703 -r 1"" -node fssflx24.fss.india
it gives following alert.
Report For Database mpaydb1
Wed Mar 11 13:57:28 IST 2015
Metric UDM 0709 (Report 1)
RESOURCE_NAME CURRENT_UTILIZATION MAX_UTILIZATION LIMIT_VALU
processes 118 359 500
sessions 124 366 784
transactions 0 6 UNLIMITED
Second Command shall pick this output and generate alert using second command.
opcmsg a=a o=o msg_text=" & X &" severity=Normal node=fssflx24.fss.india
That is how it should work.
BR,
Ramesh

How to I tell this Applescript to skip some files?

Im, using an Applescript to bashconvert a whole bunch of mv4 files to 640x480 using Handbrake CLI. I have a applescript I found somewhere changed to my parameters, and it works great. But to save time, I want the script to skip files that are already 640x480, since not all files need conversion. How would I go about that?
Here´s the script:
--on adding folder items to this_folder after receiving these_items
with timeout of (720 * 60) seconds
tell application "Finder"
--Get all m4v files that have no label color yet, meaning it hasn’t been processed
set allFiles to every file of entire contents of ("FIRSTHD:Users:jerry:Desktop:Omkodning" as alias) whose ((name extension is "m4v") and label index is 0)
--Repeat for all files in above folder
repeat with i from 1 to number of items in allFiles
set currentFile to (item i of allFiles)
try
--Set to gray label to indicate processing
set label index of currentFile to 7
--Assemble original and new file paths
set origFilepath to quoted form of POSIX path of (currentFile as alias)
set newFilepath to (characters 1 thru -5 of origFilepath as string) & "mp4'"
--Start the conversion
set shellCommand to "nice /Applications/HandBrakeCLI -i " & origFilepath & " -o " & newFilepath & " -e ffmpeg4 -b 1200 -a 1 -E faac -B 160 -R 29.97 -f mp4 –crop 0:0:0:0 crf 24 -w 640 -l 480 ;"
do shell script shellCommand
--Set the label to green in case file deletion fails
set label index of currentFile to 6
--Remove the old file
set shellCommand to "rm -f " & origFilepath
do shell script shellCommand
on error errmsg
--Set the label to red to indicate failure
set label index of currentFile to 2
end try
end repeat
end tell
end timeout
--end adding folder items to
Edited for clarity....
Since HandBreakCLI doesn't have any options to tell it not to convert if the resolution is 640x480, you have to handle that bit of logic in your script. It is easily done.
So all your script needs to do is...
get the video file resolution
If file is not at desired resolution then
convert the file
else
skip it
end if
So, as discussed here, you can use the freely available mediainfo command line tool to get the video resolution.
Here is some applescript code which you can copy/paste and run to see how it works...
set filterStr to " | grep pixels | tr -d [:alpha:][:blank:][:punct:]"
set allFiles to (choose file with multiple selections allowed)
repeat with i from 1 to number of items in allFiles
set currentFile to (item i of allFiles)
set cmdStr to "/usr/local/bin/mediainfo " & ¬
quoted form of POSIX path of currentFile & filterStr
set h to 0
set w to 0
try
set fileInfo to do shell script (cmdStr)
set {w, h} to every paragraph of fileInfo
end try
if w is not 640 and h is not 480 then
log w & " x " & h
# Convert to desired resolution....
end if
end repeat
Hope that helped.

Applescript: Break a set of lines into blocks based on a condition

I have an Automator service for uploading images and returning URLs to a text editor.
It works very well, but there's something I want to improve:
When the workflow starts, Automator will get all images in a folder and upload them to an FTP server.
Then it returns a result like below:
("http://url/0530/pic01/pic-01.jpg",
"http://url/0530/pic01/pic-02.jpg",
"http://url/0530/pic01/pic-03.jpg",
"http://url/0530/pic01/pic-04.jpg",
"http://url/0530/pic02/pic-01.jpg",
"http://url/0530/pic02/pic-02.jpg",
"http://url/0530/pic02/pic-03.jpg",
"http://url/0530/pic02/pic-04.jpg",
"http://url/0530/pic03/pic-01.jpg",
"http://url/0530/pic03/pic-02.jpg",
"http://url/0530/pic03/pic-03.jpg",
"http://url/0530/pic03/pic-04.jpg")
And the workflow gets these strings and puts them in a new text document.
Finally, here is the result in a text editor:
http://url/0530/pic01/pic-01.jpg
http://url/0530/pic01/pic-02.jpg
http://url/0530/pic01/pic-03.jpg
http://url/0530/pic01/pic-04.jpg
http://url/0530/pic02/pic-01.jpg
http://url/0530/pic02/pic-02.jpg
http://url/0530/pic02/pic-03.jpg
http://url/0530/pic02/pic-04.jpg
http://url/0530/pic03/pic-01.jpg
http://url/0530/pic03/pic-02.jpg
http://url/0530/pic03/pic-03.jpg
http://url/0530/pic03/pic-04.jpg
But I want it to be:
http://url/0530/pic01/pic-01.jpg
http://url/0530/pic01/pic-02.jpg
http://url/0530/pic01/pic-03.jpg
http://url/0530/pic01/pic-04.jpg
http://url/0530/pic02/pic-01.jpg
http://url/0530/pic02/pic-02.jpg
http://url/0530/pic02/pic-03.jpg
http://url/0530/pic02/pic-04.jpg
http://url/0530/pic03/pic-01.jpg
http://url/0530/pic03/pic-02.jpg
http://url/0530/pic03/pic-03.jpg
http://url/0530/pic03/pic-04.jpg
I think I can use AppleScript in Automator to do this before the result is sent to the text editor.
Do you have any coding advice?
===================================================
Thanks # user309603 and # mklement0 so much!!
I choose pure applescript method.
Here is my code in automator that run applescript in workflow:
on run {input, list}
set txtG to first item of input
repeat with txtLine in items 2 thru -1 of input
if txtLine contains "-01" then set txtG to txtG & linefeed
set txtG to txtG & linefeed & txtLine
end repeat
return txtG
end run
Here is a pure applescript solution.
I would prefer mklement0's awk-solution. It is much faster if you have long URL-lists.
set txt to "http://url/0530/pic01/pic-01.jpg
http://url/0530/pic01/pic-02.jpg
http://url/0530/pic01/pic-03.jpg
http://url/0530/pic02/pic-01.jpg
http://url/0530/pic03/pic-01.jpg
http://url/0530/pic03/pic-02.jpg
http://url/0530/pic03/pic-03.jpg
http://url/0530/pic03/pic-04.jpg
http://url/0530/pic04/pic-01.jpg"
set txtG to first paragraph of txt
repeat with txtLine in paragraphs 2 thru -1 of txt
if txtLine contains "-01" then set txtG to txtG & linefeed
set txtG to txtG & linefeed & txtLine
end repeat
return txtG
The following AppleScript snippet demonstrates the approach you can use in principle - I'm unclear on the exact circumstances:
If you must process the raw result:
# Sample input text.
set txt to "(\"http://url/0530/pic01/pic-01.jpg\",
\"http://url/0530/pic01/pic-02.jpg\",
\"http://url/0530/pic01/pic-03.jpg\",
\"http://url/0530/pic01/pic-04.jpg\",
\"http://url/0530/pic02/pic-01.jpg\",
\"http://url/0530/pic02/pic-02.jpg\",
\"http://url/0530/pic02/pic-03.jpg\",
\"http://url/0530/pic02/pic-04.jpg\",
\"http://url/0530/pic03/pic-01.jpg\",
\"http://url/0530/pic03/pic-02.jpg\",
\"http://url/0530/pic03/pic-03.jpg\",
\"http://url/0530/pic03/pic-04.jpg\")"
# Group (break into blocks) by files containing "-01."
set txtGrouped to do shell script ¬
"printf %s " & quoted form of txt & " | tr -d '()\"'" & ¬
" | awk -F, 'NR>1 && /-01\\./ { print \"\" } { print $1 }'" ¬
without altering line endings
If you already have a cleaned-up set of URL-only lines:
# Sample input text.
set txt to "http://url/0530/pic01/pic-01.jpg
http://url/0530/pic01/pic-02.jpg
http://url/0530/pic01/pic-03.jpg
http://url/0530/pic01/pic-04.jpg
http://url/0530/pic02/pic-01.jpg
http://url/0530/pic02/pic-02.jpg
http://url/0530/pic02/pic-03.jpg
http://url/0530/pic02/pic-04.jpg
http://url/0530/pic03/pic-01.jpg
http://url/0530/pic03/pic-02.jpg
http://url/0530/pic03/pic-03.jpg
http://url/0530/pic03/pic-04.jpg"
# Group (break into blocks) by files containing "-01."
set txtGrouped to do shell script ¬
"printf %s " & quoted form of txt & ¬
" | awk 'NR>1 && /-01\\./ { print \"\" } { print }'" ¬
without altering line endings
Uses do shell script to have awk perform the desired grouping.
Note:
without altering line endings is required to prevent AppleScript from replacing \n chars in the output with Mac-style \r line endings.
The result will have a trailing \n char, even if the input didn't. To fix this, use:
set txtGrouped to text 1 thru ((length of txtGrouped) - 1) of txtGrouped
If the goal is to always divide the list items into paragraphs grouped by four, here is an applescript you can put in an automator action:
set l to input as list
set r to ""
repeat with n from 1 to (count l) by 4
set r to r & item n of l & return & item (n + 1) of l & return & item (n + 2) of l & return & item (n + 3) of l & return & return
end repeat
return r
Clarify if instead what you need is to group all the versions of one pic together, but it won't always be 4 each, and that the script needs to check the file names to determine.
Here is a solution : This script checks the name of the parent folder for each file, when the folder name is not the same, it add a blank line.
Append the Run Shell Script action into your workflow, select the "/bin/bash" shell and select "to stdin"
Put this script in the Run Shell Script action :
awk -F/ '{f=$(NF - 1); if (NR==1) {fName=f} else if (f != fName) {print ""; fName=f} print}' < "/dev/stdin"

applescript to shell

I have created a small applescript that looks for files that match multiple strings in a certain folder, and when they are found, it returns the path to that file. in applescript language, it looks like this:
set filesExist to path of (every file in folder pathUnsorted whose name starts with (item 1 of theWords) and name contains (item 2 of theWords) and name contains (item 3 of theWords) and name contains (item 4 of theWords) and name contains (item 5 of theWords))
now, I need to convert this to a shell command, since those can run inside applescript with this command:
set filesExist to do shell script "(shell command goes here)"
Unfortunately I have no idea how to do it in a shell command...can someone help me??
Assuming that pathUnsorted is already a POSIX path:
do shell script "ls " & quoted form of (pathUnsorted & "/" & (item 1 of theWords)) & "*" & ¬
" | grep " & quoted form of (item 2 of theWords) & ¬
" | grep " & quoted form of (item 3 of theWords) & ¬
" | grep " & quoted form of (item 4 of theWords) & ¬
" | grep " & quoted form of (item 5 of theWords)
It’s theoretically possible to construct a regular expression that would match all the right files in one shot, but this is simpler.

Resources