AppleScript

RSS for tag

AppleScript allows users to directly control scriptable Macintosh applications as well as parts of macOS itself.

Posts under AppleScript tag

63 Posts
Sort by:
Post not yet marked as solved
0 Replies
167 Views
I made a simple applescript app to run a shell script and display a notification when it ends, it runs pretty well but I did't want the app appearing in the dock, so I added LSUIElement = true in the info.plist, and now the script runs but the app does not notify. Is there a way to have a background app (does not appear in the dock) with the ability to display notifications?
Posted
by lionzinho.
Last updated
.
Post not yet marked as solved
1 Replies
113 Views
I need to translate various items from a Numbers file. I used chatGPT to help me write a script which receives a cell range and translates them using Google Translate. The problem I am having is that it only translates the first and last items in the desired cell range. Please help me integrate a way to have it translate the whole range, i.e (D435:D440). Here is the script: -- Define the document file path set filePath to "file path here" -- Define the sheet, table, and cell range set sheetName to "Sheet 1" set tableName to "Table 1" set cellRange to "D429:D433" -- Function to translate text from Spanish to English using Google Translate API on translateText(textToTranslate) set baseURL to "https://translate.googleapis.com/translate_a/single?client=gtx&sl=es&tl=en&dt=t&q=" set encodedText to do shell script "python -c \"import urllib, sys; print urllib.quote(sys.argv[1])\" " & quoted form of textToTranslate set translatedText to do shell script "curl -s \"" & baseURL & encodedText & "\"" set translatedText to my parseTranslatedText(translatedText) return translatedText end translateText -- Function to parse the translated text on parseTranslatedText(translatedText) try set translatedText to quoted form of translatedText set translatedText to do shell script "python -c \"import sys, json; print json.loads(sys.argv[1])[0][0][0]\" " & translatedText return translatedText on error errMsg return "Error translating text" end try end parseTranslatedText -- Function to get the contents of a cell range on getCellRangeValues(filePath, tableName, sheetName, cellRange) set cellValues to {} tell application "Numbers" set doc to open filePath tell sheet sheetName of doc set tbl to table tableName repeat with cellRef in words of cellRange set end of cellValues to value of cell cellRef of tbl end repeat close doc saving no end tell end tell return cellValues end getCellRangeValues -- Function to set the translated text in a cell range on setTranslatedValues(filePath, tableName, sheetName, cellRange, translatedValues) tell application "Numbers" set doc to open filePath tell sheet sheetName of doc set tbl to table tableName repeat with i from 1 to count of words in cellRange set cellRef to word i of cellRange set value of cell cellRef of tbl to item i of translatedValues end repeat close doc saving yes end tell end tell end setTranslatedValues -- Main translation process try -- Get the Spanish texts from the specified cell range set spanishTexts to getCellRangeValues(filePath, tableName, sheetName, cellRange) -- Translate the Spanish texts to English set translatedTexts to {} repeat with textToTranslate in spanishTexts set translatedText to translateText(textToTranslate) set end of translatedTexts to translatedText end repeat -- Set the translated texts in the specified cell range setTranslatedValues(filePath, tableName, sheetName, cellRange, translatedTexts) display dialog "Translation completed successfully." on error errMsg display dialog "Error: " & errMsg end try The only way i have been able to bypass cell access errors is by indicating both the sheet and table numbers. Also I found a post that metions using the word 'Item' when working with a specific cell in a cell range. Thank you for your help.
Posted Last updated
.
Post not yet marked as solved
0 Replies
209 Views
I found this older post, which modifies the cursor size via AppleScript. I managed to update it to work in Ventura and Sonoma, but only in a kludgy manner. Here's the working code: set theSystemVersion to system version of (system info) tell application "System Settings" reveal anchor "AX_CURSOR_SIZE" of pane id "com.apple.Accessibility-Settings.extension" delay 1.0 tell application "System Events" if (text 1 thru 2 of theSystemVersion) is "13" then set contentView to group 2 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window "Display" of application process "System Settings" else set contentView to group 3 of scroll area 1 of group 1 of list 2 of splitter group 1 of list 1 of window "Display" of application process "System Settings" end if set theSlider to slider 1 of contentView set stash to value of theSlider if value of theSlider is 1.0 then --set value of theSlider to 4.0 repeat while value of theSlider is less than 4 increment theSlider end repeat say "Big Mouse" using "Ralph" else --set value of theSlider to 1.0 repeat while value of theSlider is greater than 1 decrement theSlider end repeat say "Tiny Mouse" using "Ralph" end if stash end tell end tell The problem is the two commented out lines: In the original post, those commands work to set the slider to an exact value. In Ventura and Sonoma, they do nothing at all—no errors, just nothing. The script runs fine, but it won't change the cursor size. The only way I got it to work is via the method you see here—repeatedly incrementing or decrementing the value of theSlider until it was either 4 or 1. (The setting of 'stash' seems completely unnecessary, but I left it there anyway.) Does anyone know why the command to set a specific slider value is failing? I'd like to be able to set it in one pass, as this method is slow and ugly. thanks!
Posted
by rgriff.
Last updated
.
Post not yet marked as solved
2 Replies
197 Views
I am trying to write a script that will perform an operation in Numbers document TEST on sheet GAMES (it’s a bunch of random number generations for a game show simulator) look at the output in cell B66 of that sheet, output it in cell B3 on another sheet called TRIALS2 then perform the operation again and output the new result on sheet TRIALS2 in cell B4 and so on for 2000 repeats. Here’s the script I have but I get an error message. The document is open and I have confirmed all the document and sheet names are correct. There is definitely something in cell B66 of Sheet GAMES in document TEST. What am I missing? tell application "Numbers" activate tell document "TEST" repeat 2000 times tell sheet "GAMES" set inputValue to value of cell "B66" end tell tell sheet "TRIALS2" set table1 to table 1 set outputColumn to column "B" set nextRow to (get cell (3 + (count of rows of table1)) of outputColumn) set value of nextRow to inputValue end tell end repeat end tell end tell Here is the error message: error "Numbers got an error: Can’t get cell "B67" of sheet "GAMES" of document "TEST"." number -1728 from cell "B67" of sheet "GAMES" of document "TEST"
Posted
by Todio.
Last updated
.
Post not yet marked as solved
0 Replies
198 Views
Hi there, I've created an Automator app running an Apple script to toggle a setting within the "systems preferences". I'm on German systems settings, so I hope you can make any sense out of the below... The intention is to toggle a setting within "Desktop & Dock" off and on again (i.e., just toggle the blue switch left and right in one go). I've attached screenshots to make clear what I mean. I've been running the following Apple Script (for only a few weeks so far), and it worked perfectly fine until just recently: on run {} repeat 2 times tell application "System Settings" to activate tell application "System Events" tell process "System Settings" click menu item "Schreibtisch & Dock" of menu "Darstellung" of menu bar 1 delay 2 tell window "Schreibtisch & Dock" click checkbox "Beim Programmwechsel Space auswählen, der geöffnete Fenster des Programms enthält" of group 9 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 end tell end tell end tell end repeat end run Now, since a few days, it keeps throwing an error message as follows: "System Events" has received an error: "group 1 of window "Desktop & Dock" of process "System Settings"" cannot be read. Invalid index. I'm not sure whether I've made an update of the MacOS in the meantime or what the reason could be. As mentioned, I'm not using the script for too much time, only a few weeks so far (mid-February). My MacOS version: Sonoma 14.3.1 Macbook Pro 16" 2021 (Apple M1 Pro) Unfortunately, my research did not point me into the right direction, I couldn't find an answer as to what exactly I'd need to change. Would much appreciate if anyone can help me set the correct index, or how to otherwise correct the script. Thanks a lot! S.
Posted
by stfnr.
Last updated
.
Post not yet marked as solved
0 Replies
199 Views
I've been using Apple Script below for gathering every title & url of opening tab from browser window. It's been works fine for a few years but it's failed recently (maybe in last two weeks). #!/usr/bin/env osascript -l JavaScript // url.js function run(arg) { arg = arg.toString() let browser = '' switch(arg) { case 'chrome': browser = 'Google Chrome' break; case 'edge': browser = "Microsoft Edge" break; default: browser = 'Google Chrome' break; } Application(browser).windows().forEach((window) => { console.log('\n\n') window.tabs().forEach((tab) => { const url = tab.url() const name = tab.name() console.log(`${name}\t${url}`) }) }) } It's been works like below (imagine that you open https://example.com on edge) $ url.js edge example.com https://example.com But I found it failed today. $ url.js edge execution error: Error: Error: Application isn't running. (-600) of course I'm make sure Edge browser up and running. Same error for Chrome. I keep my environments(os, browser etc) up-to-date but I don't know which updates affects here. Any thoughts or helps are welcome. Settings MacBook Pro M2 Sonoma 14.2.1 Edge 122 Chrome 122
Posted
by Jxck.
Last updated
.
Post marked as solved
3 Replies
287 Views
Hi there, Currently, we are working on the enterprise project that using data in ~/Library/Preferences/com.apple.dock.plist to get detail design of the original Dock (as autohide, orientation). It still has been worked until macOS 13.* as description as below: But, It has been changed in macOS 14.* Is there any alternative method to do that in macOS 14.* and later?
Posted
by lqk242.
Last updated
.
Post not yet marked as solved
1 Replies
280 Views
Hello - I have a lot of photos and videos that end up in my downloads folder from amusement park photographers and rides. The problem is that when I go to download a recent trip the amusement park website downloads everything all over again, and creates a lot of folder and files in a random sequence. So I have to go through each folder and find the "new" files that I want to add to the Photos app. Could you please help me write a script that would search through the entire downloads folder, and copy all of the files that are of photo or video type, created on or after a specified date, into a new folder within the downloads folder? This is effectively just eliminating the subfolders that the website download process created. From there I would go to the newly created folder and sort by created date to obtain the new photos I want to add to the Photos app.
Posted
by blimbert.
Last updated
.
Post not yet marked as solved
1 Replies
207 Views
Hi Community, I'm in need of some assistance with an AppleScript that increments the due date of reminders in the Apple Reminders app by one day. The reminders are set with a due date but do not have a specific time associated with them; they are just set to occur at some point during the day. The challenge I'm facing is that when I use AppleScript to add one day to the due date, the script is setting the new due date with a time of midnight. Since the original reminders do not have a time (just a date), I want the script to increment the date without adding a time. Here is the script I'm currently using: set myList to list "My List" repeat with myReminder in reminders of myList if (due date of myReminder is not missing value) then set currentDueDate to due date of myReminder set newDueDate to currentDueDate + (1 * days) set due date of myReminder to newDueDate end if end repeat end tell I am looking for a way to preserve the 'date only' attribute of the reminder when adding a day, so it does not default to a time of 00:00. Does anyone have experience with this, or can anyone provide guidance on how to accomplish this? I haven't found a way to specify 'no time' or 'all-day' in AppleScript for the Reminders app. Any help or pointers would be greatly appreciated. Thank you in advance!
Posted Last updated
.
Post not yet marked as solved
0 Replies
262 Views
Hi everyone, I'd like to write a one-off app to make some modifications to my own Music.app (aka iTunes) library. This is for content that I have stored using iTunes Match, so it's effectively a library that dates back to the iTunes years. However, from what I can see, APIs with modification support are essentially... nothing? I can get everything ready in a simple Swift command-line app using https://developer.apple.com/documentation/ituneslibrary, however I can't actually write any changes. It seems that the only modification solution available is actually still AppleScript. Everything there still works, and in theory I could do the majority of the logic using iTunesLibrary and then do the modifications with AppleScript via NSAppleScript. In order to do this, I just need an identifier that I can use to correlate items from each API. There is: iTunesLibrary: https://developer.apple.com/documentation/ituneslibrary/itlibmediaentity/1809728-persistentid AppleScript Item object for AppleScript has a "persistent ID" property which sounds like the same thing There's also an id property However, in my experiments it didn't appear that any of these correlated together Of course, I could possibly re-write the whole thing using AppleScript, but that would be slow and painful! So, posting here to see if anyone has any idea how or even if this can be done. Hope someone can help! Thanks in advance
Posted
by itsthejb.
Last updated
.
Post not yet marked as solved
1 Replies
260 Views
I made an application in Script Editor and it works as expected. But the app seems to be getting automatically deleted at random times. For example, I made it a few days ago, tested it successfully, then went back today to look for it and it was gone. Tested this multiple times. I bit more detail about my process: I wrote the app in Script editor, exported it as an Application with run-only checked and no code signing after manipulating a few things (.plist file, .icns file), I then remove extended attributes and code-sign using terminal. I have an Apple developer account that I use to code-sign: xattr -cr <path_to_app> codesign -s <my_developer_account> <path_to_app_bundle> then I copy the app into my Applications folder and test it successfully a day or more later, the app is gone (and I haven't even opened it again) Ventura 13.1, Mac Book Pro 2021
Posted
by jiffjaff.
Last updated
.
Post not yet marked as solved
1 Replies
253 Views
A TextEdit RTD or RTFD document will save various document properties that you access and edit by selecting: File > Show Properties > Is is possible to retrieve these properties on a currently open document via AppleScript and place them in the clipboard? I am interested in storing some text in the Comments and later being able to copy it to the clipboard without having to manually open the properties window, selecting the text and copying it.
Posted Last updated
.
Post not yet marked as solved
1 Replies
249 Views
Hi!! I tried posting about my issue on the macscripter forum, but haven't receieved any love there so I thought I would try giving it a go here. I'm trying to help automate my wife's business which uses mac and am having difficulty getting an applescript to trigger by mail rule. I am not an apple guy and myself use linux, so maybe i am overlooking something basic that I need to enable? I gave the mail.app full disk access in the security panel. If I set a rule, like to move a message to another mail folder it executes without problems. However, I can't get any applescripts to run triggered by mail rule, even something very simple...like writing to the display or logging an event. I looked through the console and saw no error messages or any evidence even that the applescript tried to run. We are running the latest mac sonoma. I asked chatgpt for help trying to debug what was wrong but it ran out of suggestions for me as well. This problem is driving me crazy, I can't understand how a nice new mac computer I bought for my wife's business is failing at the most basic task... arrrgh. Any help greatly appreciated!!!
Posted
by rubinglen.
Last updated
.
Post not yet marked as solved
0 Replies
290 Views
Hi. I have some Apple Script (below) that opens new Safari windows. How would I specify a particular Profile to use when opening the new window? Thanks! tell application "Safari" set mydocument to (make new document at end of documents) activate end tell
Posted Last updated
.
Post marked as solved
1 Replies
441 Views
Hey! Im new here and currently learning iOS/macOs development (SwiftUI), so...take me easy :) I want to create a simple macOS app to let user set time until computer power off. I found an example with AppleScript and use it on my app, but I found that App won't run with Sandbox enabled, and to deploy app on AppStore it show me that Sandbox must be enabled. The script I want to use: 'tell application "System Events" to shut down' I found some examples that add script onAbsolute path, but after I do that, it won't let me to distribute the app, only export to run local. It is any way to make script running (no matter, if app ask for user permission/admin pass) ?
Posted Last updated
.
Post not yet marked as solved
0 Replies
368 Views
Hallo, I created an AppleScript for my Synology NAS and saved it as NAS.app. This starts the server and mounts the network drives. If the script is run again when the server is already online, it can shut down the Mac and server. That works very well. But the next time I start the Mac, this app is immediately active again and may have to be closed manually. I'm now looking for a way to prevent this. How can I kill the app after issuing the shutdown command? Here is the part with the Mac shutdown code. set ProcNm_ to name of every application process whose visible is true end tell repeat with i_ from 1 to count items of ProcNm_ set TarProc_ to item i_ of ProcNm_ --display dialog TarProc_ try if TarProc_ is not "Finder" then tell application TarProc_ to quit "without saving" end if end try end repeat try do shell script "sudo /sbin/shutdown -h +1" end try quit I hope somebody can help me. b. r. Binh
Posted
by BinhDiez.
Last updated
.
Post not yet marked as solved
0 Replies
400 Views
Does anyone have a simple example for a scriptable App in Swift in Xcode for macOS? I mean one that exposes objects and functions to applescript? I've checked all examples that I could find. But for me they get me confused about whether they are accessing the actual and already existing instance of a class or whether they are creating an instance with AppleScript, or just using the class definition to call some function. Also the way they are defined in the .sdef file does not help. It would be great if someone had a really simple example, stripped down to bare minimum that 1) defines a class with a single function, 2) creates an instance of the class inside appDelegate and then 3) the function of the specific instance is called using appleScript. 4) It would be great if the function could do something in the GUI, such as change a text of a Label.
Posted
by Stan1.
Last updated
.
Post not yet marked as solved
0 Replies
352 Views
My grand plan is to have a rule in Apple Mail that automatically adds a custom header to each message. This rule would be last and each of the rules before that last rule would check for the presence of this custom header and therefore no longer work for incoming mail. So the plan is to have a small AppleScript that adds this header but I am running into a block which hopefully someone can help we with. I have the following pieces of code: tell application "Mail" activate set myHeader to make new header with properties {name:"X-MySecretHeader", content:"It's been set"} end The last statement fails with the following error: Mail got an error: Can’t make or move that element into that container. I then changed the code slightly: tell application "Mail" activate set theMessage to get item 1 of (get selection) set theHeaders to headers of theMessage set myHeader to duplicate item -1 of theHeaders end Mail got an error: Headers can not be copied. That last error seems to indicate that I'm trying to do something which cannot be done. I have also tried to do this in a tell block to theMessage but that did not change anything. Anyone has an idea?
Posted
by FredA.
Last updated
.
Post not yet marked as solved
3 Replies
2.2k Views
I know that zoom.us is not technically scriptable with Applescript but I have gotten so tired of repeatedly doing the same things when I set up a Zoom session that I started trying to automate the process using System Events and tell process. I was surprised by how much I could automate but then ran into a couple of brick walls. If anyone else is interested in this I'd like to brainstorm on the forum to see how much is possible. Here's some scripting steps I have been playing with. Part of what I decided to do in order to simplify the data entry involved in scheduling a new meeting and sending an email to the client was to run everything from Filemaker. The variables shows as $theMonth, $theDay, etc are being set in FileMaker before the Perform Applescript step so this is not a viable Applescript. You could use three separate Applescript dialog boxes to get the name date and time. The key code 48 is a tab to move between fields on the zoom interface. Key code 76 is the enter key which will OK the automatic entries in the Calendar so that I can quit Calendar and get back to zoom. There seems to be no way to stop zoom from automatically posting a new session in the Calendar. tell application "zoom.us" to activate tell application "System Events" tell process "zoom.us" repeat until window "Schedule Meeting" exists click menu item "Schedule Meeting..." of menu "Zoom.us" of menu bar 1 delay 0.2 end repeat set topicField to text field 1 of "Schedule Meeting" keystroke "Whatever You Want It To Be"" key code 48 keystroke $theMonth as text key code 48 keystroke $theDay as text key code 48 keystroke $theYear as text key code 48 keystroke $theHour as text key code 48 keystroke $theMinute as text key code 48 keystroke $theMonth as text key code 48 keystroke $theDay as text key code 48 keystroke $theYear as text key code 48 keystroke ($theHour + 1) as text key code 48 keystroke $theMinute as text Delay 2 key code 36 key code 76 key code 76 end tell end tell tell application "Calendar" to quit tell application "zoom.us" to activate tell application "System Events" tell process "zoom.us" repeat until window "Schedule Meeting" exists click menu item "Schedule Meeting..." of menu "Zoom.us" of menu bar 1 delay 0.2 end repeat select button ?????? end tell end tell The zoom.us interface for Meetings has what appear to be buttons for each scheduled meeting. I am trying to figure out how to select the right button so that I can then copy the invitation to put in an email. Tabbing does not let me select the button. If anyone has any ideas, I'd love to hear them, and if my further experiments produce any results, I'll post them. Thanks
Posted
by RGPatt.
Last updated
.