Can strings from AppleScript be converted into arrays in TypeScript?

I'm working on executing AppleScript in TypeScript, which returns a string that actually contains an array. Is there a way to convert this string into an array of items in TypeScript?

Below is the code snippet for obtaining a string of an array from AppleScript:

import { Detail } from "@raycast/api";
import {runAppleScript} from 'run-applescript';
import { spawnSync } from "child_process";


async function spacesArray(): Promise<String> {
  const out = spawnSync("defaults read com.pradeepb28.spacesfor myNewArray", { shell: true });
  return String(out.output[1]).trim();
}

The result (in string format) looks like this:

(
    abc,
    abcd,
    abcdef
)

Answer №1

extract the necessary data from out.output array and format it by separating with commas.

Answer №2

The execute-applescript module communicates with the command-line osascript, but it struggles with handling complex arguments and results.

To parse the output effectively, consider adding the -ss flag to your osascript command. This will format the output in AppleScript literals for reliable parsing. The default output format is often unclear.

Alternatively, you can have your AppleScript code convert a list of strings into a JSON string for easier parsing using JSON.parse(). While AppleScript lacks built-in JSON support, you can utilize Cocoa's NSJSONSerialization class through the AppleScript-ObjC bridge. Though more effort is required, this approach ensures robustness without introducing additional dependencies.

Another option is to bypass osascript altogether and call NSAppleScript from JS via the objc bridge. Although the objc bridge may be rough around the edges, it could suffice for your requirements despite the tedious process of unpacking NSAppleEventDescriptors.

For those looking to eliminate AppleScript, utilizing nodeautomation directly from JS allows for application scripting. However, note that while nodeautomation is effective (used in personal projects), no warranty or free support is provided.

We understand there isn't a perfect solution, especially given the challenges faced by Mac Automation. Despite its shortcomings, the fact that it continues to function is somewhat remarkable.

Answer №3

Through a manual parser, I successfully resolved the issue:

  • eliminated spaces, removed \n, and got rid of open brackets

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Developing JSON with the use of jQuery

My latest project involves creating an application that converts HTML tables to JSON format. The code is functioning properly, but I've encountered an issue when the td or th elements contain inner components like span or other child elements. While I ...

The array value will remain as 0 when printed, even if it has been modified in C

I recently created a custom data type called typedef unsigned char byte;, and proceeded to declare an array of this type by using byte mem[255];. To initialize the first value, I used mem[0] = 0x10100000;. However, when I attempt to print this value usin ...

Spend three minutes on the page with the help of AJAX

Can a page be requested using AJAX and remain on that page for approximately 3 minutes before the ajax function returns, allowing for dynamic content to fully load? ...

Converting jQuery drag and drop functionality to Angular: A step-by-step guide

I have implemented drag and drop functionality using jquery and jquery-ui within an angular project. Below is the code structure: Index.html, <!doctype html> <html lang="en"> <head> <link href="//code.jquery.com/ui/1.10.3/themes/ ...

Having trouble with your JavaScript slideshow?

I've encountered an issue while working on a simple JavaScript picture slideshow. Strangely, when I copy the code directly from the website where I learned it, it works perfectly fine. However, if I try to recreate the exact same code by hand, it does ...

"In the combined declaration of 'DepartmentListComponent', all individual declarations must either be exported or kept local." - error message regarding routing components in TypeScript file

I followed a video tutorial to write this code, but it's not working as expected and is throwing some errors. Is it possible that the tutorial is outdated and using an older methodology? The code seems to be hard-coded without using any services. Her ...

Using JQuery to make POST requests is successful, however, utilizing the XMLHttpRequest object to make

Currently, I am attempting to make a POST request from Javascript to my server in PHP but without utilizing jQuery. The following code successfully sends the required data to the database: var msg = {}; msg['name'] = 'joe'; msg['m ...

How to extract cookie value in a node.js application using socket.io

How can I retrieve cookie values in a node server using socket.io? Whenever a socket sends a message to the server, I need to check the value of the cookie. However, I only want to obtain the cookie value and not the session cookie as done in Express. r ...

Why is the appsecret_proof invalid?

Currently, I am in the process of developing a new application. I am utilizing the Javascript API for logging in (v2.4) and the latest version of the PHP API (v5). To test the functionality of my app, I have created a Test App. With the Javascript API, I h ...

When the jquery is still running, HTML initiates the loading of a new page

When I click on an anchor HTML tag with a link, I want to change the value of a variable in jQuery. However, even though the tag loads a new page, the variable's value remains unchanged. After doing some research online, I discovered that this issue ...

Tips for populating web view fields on Android using a combination of Java and JavaScript

Encountering an issue while attempting to populate fields in my android web view. Consistently receiving this error message 01-28 05:06:22.980: E/Web Console(2827): Uncaught TypeError: Cannot set property 'value' of null at null:1 Despite search ...

What is the best way to revert the Highcharts bar color back to its default color?

I am currently working with Highcharts version 4.1.10 In my bar chart, I have implemented a feature where the color of the bar changes when its value exceeds a certain threshold. //This function is called at regular intervals by a timeout function myTime ...

Angular AOT and Typescript mapping tutorial

I have encountered a problem with using TypeScript map in my Angular application. When I run the application using 'ng serve', everything works fine. However, if I use 'ng serve --aot', the maps are not functioning as expected. Despite ...

Retrieving a value from an input field within a table cell using jQuery

Looking for some help with a complex HTML Table structure like this one. <table id="items"> <tr class="total_up"> <td colspan="2" class="blank"></td> <td colspan="2" class="total-line">Total</td> ...

Encountering a 500 error with Ajax in Laravel 5.2

Rediscovering Laravel 5.2 - Starting a Fresh Application After getting back into Laravel, I encountered a challenge with an AJAX post request. Essentially, when you reorder the list, AJAX is triggered to modify the order and save it in the database. Desp ...

Using JS/AJAX to update a section of a webpage when a button is clicked

Currently, I am working on developing a generator that will display a random line from a .txt file. Everything is going smoothly so far, but I have encountered an issue - I need a specific part of the page to refresh and display a new random line when a ...

Establishing session management for tasks in Node.js

I'm currently developing a web application using Node JS and encountering an issue with the session store in req. For my sessions to work, I have set up my app.js like this: // Enable sessions app.use(session({ secret: '***********', ...

Restricting a checkbox to a maximum of 5 checkmarks

In a multi-column table, each column is represented by a checkmark. I want to limit the ability to tick a checkmark to only 5 checkmarks. Here is the code that has been implemented: <tbody> <ng-container *ngFor="let col of testData" ...

What is the best way to retrieve information from an API endpoint?

I have set up my frontend using nextjs (running on port 3000) and am attempting to fetch data from API endpoints in strapi (running on port 1337). How can I configure my frontend application to successfully retrieve data from the backend? Is there a way to ...

Unable to connect HTML data to the view section using angular.js

I am trying to incorporate some HTML content into my view using angular.js. time.html: <table class="table table-bordered table-striped table-hover" id="dataTable"> <tr> <td width="100" align="center">Time <i class="fa fa-long- ...