Applescript process name `Can’t get name of "login".` - process

tell application "Terminal"
do script ""
set processList to processes of window 1
repeat with p in processList
set n to name of p
end repeat
activate
end tell
trying to get the process's name and it's failing help please

This will give you both the processes and names of each Terminal window.
set processList to {}
set windowNames to {}
tell application "Terminal"
set theWindows to windows
repeat with i from 1 to count of theWindows
set thisItem to window i's id
tell window id thisItem
set {end of processList, end of windowNames} ¬
to {processes, name}
end tell
end repeat
end tell

Related

How to click on this webpage button with AppleScript

I am having trouble clicking on the "search" button on a particular website. The website is a subscription service, so I am attaching a picture of the page pulled up in "inspect" mode as well as my code.
My code:
set myURL to "https://www.uptodate.com/contents/search"
tell application "Safari"
activate
make new document with properties {URL:myURL}
end tell
tell application "System Events"
repeat until exists (UI elements of groups of toolbar 1 of window 1 of application process "Safari" whose name = "Reload this page")
delay 0.3
end repeat
end tell
inputByID("tbSearch", "myVar")
clickClassName("newsearch-submit", 0)
###InputByID###
to inputByID(theId, theValue)
tell application "Safari"
do JavaScript "document.getElementById('" & theId & "').value ='" & theValue & "';" in document 1
end tell
end inputByID
###ClickByClass###
to clickClassName(theClassName, elementnum)
tell application "Safari"
do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1
end tell
end clickClassName
As an alternative, you can use UI Scripting, where System Events preforms some keystrokes.
The following example AppleScript code works for me in macOS High Sierra1:
set myURL to "https://www.uptodate.com/contents/search"
tell application "Safari"
activate
make new document with properties {URL:myURL}
end tell
tell application "System Events"
repeat until (accessibility description of ¬
button 1 of UI element 1 of every group of toolbar 1 of window 1 of ¬
process "Safari" whose name = "Reload this page") contains "Reload this page"
delay 0.5
end repeat
end tell
tell application "Safari"
do JavaScript "document.getElementById('tbSearch').value ='PPE';" in document 1
end tell
tell application "System Events"
keystroke space
key code 51 -- # Press the delete key to remove the space.
keystroke return
end tell
1 NOTE: For macOS Mojave and later, change UI element 1 to UI element 2 in the repeat loop above.
Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.
I played around with this and of course not having a subscription cannot truly test it but perhaps you can see if this works for you - it seemed to be working:
tell application "Safari"
--activate --can work with or w/o Safari in foreground
set myURL to "https://www.uptodate.com/contents/search"
make new document with properties {URL:myURL}
delay 5 -- just waits for (hopefully) the page to load
set d to do JavaScript "document.forms[0][0].value = 'joint pain'" in document 1
do JavaScript "document.forms['searchForm'].submit();" in document 1
end tell

Rest of AppleScript is ignored when I use a variable in posix path

I'm using AppleScript in Automator to copy a page's source and save it to a file. For some reason when I use a variable (titleVal) in the posix path, the rest of my code in my loop is ignored, including the file that never gets written.
I updated the code before with my full AppleScript in case it has to do with more than the few lines I had before. I'm using Automator with specified Finder items in this order: "urlList.txt" and fileList.txt".
on run {input, parameters}
set updateCount to 0
read (item 1 of input)
set ps to paragraphs of the result
set tot to count ps
set TLFile to (("Users:Admin:Desktop:download captions:") as text) & "fileList.txt"
set TLLines to paragraphs of (read file TLFile as «class utf8»)
tell application "Safari"
reopen
activate
end tell
repeat with i from 1 to tot
set p to item i of ps
if p is not "" then
try
tell application "Safari"
tell front window
set r to make new tab with properties {URL:"https://www.youtube.com/timedtext_editor?v=" & p & "&lang=en&name=&kind=&contributor_id=0&bl=vmp&action_view_track=1"}
set current tab to r
set titleVal to item i of TLLines
set updateCount to updateCount + 1
do shell script "echo The value: " & updateCount
delay 2
do JavaScript "document.getElementById('movie_player').outerHTML = ''" in current tab
do JavaScript "document.getElementById('creator-page-sidebar').outerHTML = ''" in current tab
do JavaScript "document.getElementById('footer').outerHTML = ''" in current tab
delay 3
do JavaScript "document.getElementsByClassName('yt-uix-button yt-uix-button-size-default yt-uix-button-default action-track-button flip yt-uix-menu-trigger')[0].click()" in current tab
delay 1
do JavaScript "document.getElementById('aria-menu-id-2').getElementsByTagName('ul')[0].getElementsByTagName('li')[5].getElementsByTagName('a')[0].click()" in current tab
delay 4
-- using a variable in path1 is where it screws up. try changing it to another variable value and it will have the same effect.
set myString to source of current tab
set path1 to "/Users/Admin/Desktop/download captions/downloadedCaptions/" & titleVal & ".srt"
say path1
set newFile to POSIX file path1
--set newFile to POSIX file "/Users/Admin/Desktop/download captions/downloadedCaptions/test.xml.srt"
open for access newFile with write permission
write myString to newFile
close access newFile
-- i have exit repeat here to only test the first loop
exit repeat
end tell
end tell
end try
end if
end repeat
end run
Without a variable works fine, but I need the variable to make the script work properly in a loop. I've checked the value of the var. I also tried "& quoted form of titleVal &".
Update: When I remove the try/end try as suggested to get the error, the error is:
The action “Run AppleScript” encountered an error: “Safari got an error: Can’t get POSIX file "/Users/Admin/Desktop/download captions/downloadedCaptions/test.srt" of window 1.”
The error occurs because you are going to write the file in the tell window block of Safari which cannot work.
I recommend to use a separate handler. Put the on writeFile handler outside of the on run handler. I added reliable error handling and the data are saved UTF-8 encoded.
on writeFile(theData, fileName)
set newFile to "/Users/Admin/Desktop/download captions/downloadedCaptions/" & fileName & ".srt"
try
set fileDescriptor to open for access newFile with write permission
write theData to fileDescriptor as «class utf8»
close access fileDescriptor
on error
try
close access newFile
end try
end try
end writeFile
and call it (replace the part of your code from delay 4 to the end)
delay 4
-- using a variable in path1 is where it screws up. try changing it to another variable value and it will have the same effect.
set myString to source of current tab
my writeFile(myString, titleVal)
exit repeat
end tell
end tell
end try
end if
end repeat
end run
This was fixed very easily by changing one line:
set newFile to POSIX file path1
to:
set newFile to (path1 as POSIX file)
But I don't know why. Seems like a bug since it worked without the variable. Please provide any details in the comments why this works over the original set newFile line.

Open an unspecifiable Folder with applescript

I'm trying to write an AppleScript for a Keyboard Maestro Macro, which opens the pictures folder of a camera SD or CF Card in an existing Finder window.
This is my current code, which opens the mounted Volume.
tell application "Keyboard Maestro Engine"
set KMVarPath to get value of variable "path"
end tell
set the_string to "/Volumes/" & KMVarPath
set the_path to (POSIX file the_string) as string
tell application "Finder"
activate
if window 1 exists then
set target of window 1 to the_path
else
reveal the_path
end if
end tell
The problem is those folders are called ie 276ND2XS or 105ND800. I'd like to specify the 'suffix' (ND2XS/ND800) and open the folder with the highest 'prefix' number.
Is there a way to do that?
And for convenience, is there a way to check, whether the volume is an SD or CF Card? Or do I have to check via the name (NIKON D2XS / NIKON D800)?
I suggest you to look for shell command : system_profiler SPStorageDataType
You can use it in a "do shell script" command in your applescript.
This command gives you, among others, the name all connected storage types (USD, SD card, hard drive), the mount point (example /volumes/myDisk), the physical drive device name and media name, and the protocol (USB, SATA, ATA,...). I don't have CF Card to test, but it should give you a way to detect. As example, When I use SD card, Media Name is "APPLE SD Card Reader Media".
Once you know the volume, you can get the folder with highest counter name with :
set the_path to choose folder -- this is just for my test ! replace by your path "Volumes:..." as alias
set the_Folder to ""
set the_Highest to 0
tell application "Finder"
set my_List to name of every folder in the_path whose (name contains "D2XS") or (name contains "ND800")
repeat with a_folder in my_List
try
set the_Num to (text 1 thru 3 of a_folder) as integer
on error
set the_Num to 0
end try
if the_Num > the_Highest then
set the_Highest to the_Num
set the_Folder to a_folder
end if
end repeat
end tell
log "num=" & the_Num
log "fodler=" & (the_Folder)

AppleScript Automation - Bulk uploading files in folder to a single-upload form

(First time with AppleScript...) I'm trying to bulk upload files from a local folder to a server via a single-upload form (legacy serverside software behind ddos wall, no control over it)
As I understand:
I can loop through each file in the filesystem.
With each file: Invoke "tell" Safari"
Invoke javascript to "click" a button by ID
file upload dialog, select the file to upload (?)
I'm having some trouble with syntax in implementing that...
(Also, if that's not the right/best approach, please provide a better one below!)
on run
tell application "Finder"
set mlist to (every file of folder "Macintosh HD:Users:username:filestouploadfolder") as alias list
repeat with this_file in mlist
tell application "Safari"
activate
do JavaScript "document.getElementById('selectToOpenFileDialog').click();" in document 1
choose file this_file
end tell
end repeat
end tell
return 0
end run
Hacked up a solution though it could probably be more elegant
on run
tell application "Finder"
set mfolder to "Macintosh HD:Users:yosun:png:"
set myFiles to name of every file of folder mfolder
end tell
repeat with aFile in myFiles
tell application "Safari"
activate
delay 1
do JavaScript "document.getElementById('addDeviceTargetUserView').click();" in document 1
delay 1
do JavaScript "document.getElementById('targetDimension').value=10;" in document 1
do JavaScript "document.getElementById('targetImgFile').click();" in document 1
end tell
tell application "System Events"
keystroke "G" using {command down, shift down}
delay 1
keystroke "~/png/" & aFile as string
delay 1
keystroke return
delay 1
keystroke return
delay 1
end tell
tell application "Safari"
activate
delay 1
do JavaScript "document.getElementById('AddDeviceTargetBtn').click();" in document 1
end tell
delay 10
end repeat
end run

Applescript variable not usable in other script

I have made an applescript that sets variable 'msshutdown' to yes and then shuts down the computer. Now I have another script exported as an application added as a login item that starts up the program 'MainStage' if 'msshutdown' is set to yes and afterwards sets 'msshutdown' to no.
This is all because I want the computer to don't launch any apps at login, unless I shut it down using the first script.
But it seems the second script can't find the variable 'msshutdown'. How do I make the second script read thew status of the variable in the first script and then edit it?
First script:
set msshutdown to yes
tell application "Finder"
shut down
end tell
Second script:
if msshutdown is yes then
tell application "MainStage 3"
activate
end tell
set msshutdown to no
end if
Easiest solution is to write the variable to a file, then read it when needed. A simple text file will do the job.
First Script:
writeVar("yes")
tell application "Finder"
shut down
end tell
on writeVar(theVar)
do shell script "echo " & quoted form of (theVar as text) & " > ~/varFile.txt"
end writeVar
Second Script:
if readVar() is "yes" then
tell application "MainStage 3"
activate
end tell
writeVar("no")
end if
on writeVar(theVar)
do shell script "echo " & quoted form of theVar & " > ~/varFile.txt"
end writeVar
on readVar()
do shell script "cat ~/varFile.txt"
end readVar
Save the script below into the ~/Libraries/Script Libraries folder with the name shutdownStore
use AppleScript version "2.3"
use scripting additions
property shutDownCacheName : "shutdownStore"
property shutdownCache : missing value
to saveShutDownStatus(theShutDownStatus)
set cachePath to ((path to library folder from user domain as text) & "caches:" & "net.mcusr." & my shutDownCacheName)
set shutdown of my shutdownCache to theShutDownStatus
store script my shutdownCache in cachePath replacing yes
end saveShutDownStatus
on loadShutDownStatusFromScriptCache()
set cachePath to ((path to library folder from user domain as text) & "caches:" & "net.mcusr." & my shutDownCacheName)
local script_cache
try
set my shutdownCache to load script alias cachePath
on error
script newScriptCache
property shutdown : false
end script
set my shutdownCache to newScriptCache
end try
return shutdown of my shutdownCache
end loadShutDownStatusFromScriptCache
on getShutDownStatus()
set last_shutDownStatus to loadShutDownStatusFromScriptCache()
return last_shutDownStatus
end getShutDownStatus
Use this from your scripts like I have modified them:
First Script:
use AppleScript version "2.3"
use scripting additions
use mss : script "shutdownStore"
set msshutdown to yes
saveShutDownStatus(msshutdown) of mss
tell application "Finder"
shut down
end tell
Second Script:
use AppleScript version "2.3"
use scripting additions
use mss : script "shutdownStore"
set msshutdown to getShutDownStatus() of mss
if msshutdown is yes then
tell application "MainStage 3"
activate
end tell
set msshutdown to no
saveShutDownStatus(msshutdown) of mss
end if