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

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.

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

I suspect the fact that you’re calling it via NSAppleScript is a red herring. Can you post the 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.

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

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

//
//  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 */

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.

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 was right.
Following your advice, I modified the code, and the problem was finally resolved.
Shane! Thank you very much. You saved my day :smiley:

—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\