Thursday, February 25, 2021

#1 2020-10-20 04:53:23 pm

farmingmoney
Member
Registered: 2020-01-13
Posts: 20

Using AppleScriptObjC in NSAppleScript based code

Machine translation was used.
I ask for your understanding even if there are some problems with the phrase I describe.

I am developing an Apple script-embedded app using xcode

Applescript:

NSAppleEventDescriptor* returnDescriptor = NULL;
NSAppleScript* scriptObject3 = [[NSAppleScript alloc] initWithSource:s3VideoString];

This script worked fine until AppleScriptObjC was included in this script.

However, there is a problem now
Since the AppleScriptObjC framework is needed to implement the on modifierKeysPressed() function, the following statement is put in the Apple script.

Applescript:

use AppleScript version \"2.4\"\n\
use scripting additions\n\
use framework \"Foundation\"\n\
use framework \"AppKit\"\n\

However, the following runtime error occurs when executing
"can't make current application into type file"

I think probably it is not possible to reference the library
I ask for help

xcode 11.5
catalina 10.15.7

Offline

 

#2 2020-10-20 05:07:19 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6565

Re: Using AppleScriptObjC in NSAppleScript based code

I suspect the fact that you're calling it via NSAppleScript is a red herring. Can you post the code?


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#3 2020-10-20 06:23:49 pm

farmingmoney
Member
Registered: 2020-01-13
Posts: 20

Re: Using AppleScriptObjC in NSAppleScript based code

The 400 line source code is shortened and expressed as follows.

When I first dealt with this problem, I was just trying to find the problem only in the code I added (isForceCancelWorkflow).

By the way, while writing this article, I checked the code again and found something strange.
The part where the error occurs is not that the problem occurred in the code I added, but a problem occurred in the function (readCommaSeparatedValuesFile) that worked well before.
In fact, I do not know the causal relationship of this problem.

An error(-1700) occurs while calling this function (on readCommaSeparatedValuesFile())
The following error occurs when parsing the csv file.

"can't make current application into type file"



The interesting thing is that if I delete the 3 lines below and run it, the script works normally with csv.

use AppleScript version \"2.4\"\n\
use framework \"Foundation\"\n\
use framework \"AppKit\"\n\



How does the following xcode warning message about the s3VideoString variable currently relate to this problem?

String literal of length 11602 exceeds maximum length 4095 that ISO C99 compilers are required to support



Applescript:

//
// s3Video.h
//

#ifndef s3Video_h
#define s3Video_h

NSString* s3VideoString=\
@"\
\n\
use AppleScript version \"2.4\"\n\
use framework \"Foundation\"\n\
use framework \"AppKit\"\n\
use scripting additions\n\
\n\
\n\
my s3Video()\n\
\n\
\n\
---\n\
--- s3Video\n\
---\n\
on s3Video()\n\
\n\
-- .\n\
-- .\n\
-- .\n\
set csvRecords to my readCommaSeparatedValuesFile(csvFile)\n\
-- .\n\
--- super loop\n\
repeat\n\
-- .\n\
-- .\n\
-- user cancel by key intervention \n\
if my isForceCancelWorkflow() then\n\
return --exit\n\
end if\n\
-- .\n\
-- .\n\
end repeat\n\
end s3Video\n\
\n\
\n\
\n\
---\n\
--- read comma-separated string file\n\
--- https://iworkautomation.com/numbers/table-read-file.html\n\
—\n\
on readCommaSeparatedValuesFile(thisCSVFile)\n\
try\n\
set dataBlob to (every paragraph of (read POSIX file thisCSVFile))\n\
set the tableData to {}\n\
set AppleScript's text item delimiters to \",\"\n\
repeat with i from 1 to the count of dataBlob\n\
set the end of the tableData to (every text item of (item i of dataBlob))\n\
end repeat\n\
set AppleScript's text item delimiters to \"\"\n\
return tableData\n\
on error errorMessage number errorNumber —:(\n\
say \"An error has occurred\"\n\
display notification errorMessage as text\n\
say errorMessage as text using \"samantha\"\n\
say number as text using \"samantha\"\n\
say errorNumber as text using \"samantha\"\n\
set AppleScript's text item delimiters to \"\"\n\
error errorMessage number errorNumber\n\
end try\n\
end readCommaSeparatedValuesFile\n\
—\n\
—\n\
--\n\
-- cancel Workflow\n\
--\n\
on isForceCancelWorkflow()\n\
say \"begin isForceCancelWorkflow\"\n\
tell application \"System Events\"\n\
set modifierKeysDOWN to {command_down:false, option_down:false, control_down:false, shift_down:false}\n\
set modifierKeysDOWN to my modifierKeysPressed()\n\
if shift_down of modifierKeysDOWN is true then\n\
say \"Cancel operation\"\n\
return true --cancel\n\
else\n\
return false --OK\n\
end if\n\
end tell\n\
say \"end isForceCancelWorkflow\"\n\
end isForceCancelWorkflow\n\
\n\
--\n\
-- Checks whether a key is pressed\n\
--\n\
on modifierKeysPressed()\n\
say \"begin modifierKeysPressed\"\n\
set modifierKeysDOWN to {command_down:false, option_down:false, control_down:false, shift_down:false}\n\
\n\
set |⌘| to current application\n\
set currentModifiers to |⌘|'s class \"NSEvent\"'s modifierFlags()\n\
\n\
tell modifierKeysDOWN\n\
set its option_down to (currentModifiers div (get |⌘|'s NSAlternateKeyMask) mod 2 is 1)\n\
set its command_down to (currentModifiers div (get |⌘|'s NSCommandKeyMask) mod 2 is 1)\n\
set its shift_down to (currentModifiers div (get |⌘|'s NSShiftKeyMask) mod 2 is 1)\n\
set its control_down to (currentModifiers div (get |⌘|'s NSControlKeyMask) mod 2 is 1)\n\
end tell\n\
\n\
say \"end modifierKeysPressed\"\n\
return modifierKeysDOWN\n\
end modifierKeysPressed\n\
"
;

#endif /* s3Video_h */

Last edited by farmingmoney (2020-10-20 06:55:48 pm)

Offline

 

#4 2020-10-20 07:13:13 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6565

Re: Using AppleScriptObjC in NSAppleScript based code

farmingmoney wrote:



Applescript:

set dataBlob to (every paragraph of (read POSIX file thisCSVFile))\n\



I suspect that's your problem. ASObjC doesn't allow using terms like alias, file and POSIX file as specifiers -- they have to be used as coercions. So you would need to use something like "(thisCSVFile as POSIX file)" instead.

How does the following xcode warning message about the s3VideoString variable currently relate to this problem?



It doesn't. That's just telling you your string literal is longer than it should be for C99 compliance — nothing to do with AppleScript in any way.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#5 2020-10-20 10:22:48 pm

farmingmoney
Member
Registered: 2020-01-13
Posts: 20

Re: Using AppleScriptObjC in NSAppleScript based code

Shane was right.
Following your advice, I modified the code, and the problem was finally resolved.
Shane! Thank you very much. You saved my day big_smile

Applescript:

—Before modification
set dataBlob to (every paragraph of (read POSIX file thisCSVFile))\n\

—After modification
tell application \"Finder\" to set myFile to (POSIX path of thisCSVFile)\n\
set dataBlob to (every paragraph of (read myFile))\n\

Last edited by farmingmoney (2020-10-21 03:09:49 am)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)