Sunday, May 31, 2020

#1 2020-02-26 01:55:02 pm

Marius_Magnificus
Member
Registered: 2019-05-05
Posts: 16

Including a .txt into a AppleScriptobjc Xcode project

Hi all,

I'm trying to include a .txt within my AppleScriptobjc Xcode project but didn't found the way to do it in case that it is possible.

My intention is to bundle it and create a standalone application that does not have the necessity of look for any external resource outside the application.

In case that it possible I'd much appreciate to know what are the steps I should follow to integrate it and how to reference it.

Best,

Marius.


Filed under: reference, xcode, bundle, .txt

Offline

 

#2 2020-02-26 02:24:58 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11686
Website

Re: Including a .txt into a AppleScriptobjc Xcode project

Of course it's possible.

• Press ⌥⌘A or drag the file into the Project Navigator
• You get the path to the file with


Applescript:

set theFile to current application's NSBundle's mainBundle()'s pathForResource:"MyFile" ofType:"txt"


regards

Stefan

Offline

 

#3 2020-02-26 04:02:18 pm

Marius_Magnificus
Member
Registered: 2019-05-05
Posts: 16

Re: Including a .txt into a AppleScriptobjc Xcode project

StefanK wrote:

Of course it's possible.

• Press ⌥⌘A or drag the file into the Project Navigator
• You get the path to the file with


Applescript:

set theFile to current application's NSBundle's mainBundle()'s pathForResource:"MyFile" ofType:"txt"




Thank you for your quick response,

I have not bundle the application yet since I'm still developing it.

I tried different things following your steps but with no success.
When I drag the .txt into the project folder I'm prompt to select a

'Destination': Copy items if needed (I check it)
'Added folders': (I check Create folder references)
'Add to targets': (I check my project)

And here it is the code I've used in the AppDelegate file to test it:

[applescript]set theFile to current application's NSBundle's mainBundle()'s pathForResource:"MyFile" ofType:"txt"
display dialog theFile
# It yields "msng"

set theFile to theFile as «class furl»
set theFileList to paragraphs of (read theFile as «class utf8»)

# But I received the following error message:
Can’t make missing value into type «class furl». (error -1700)[/AppleScript]

Thank you in advance.

Marius

Offline

 

#4 2020-02-26 04:27:00 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11686
Website

Re: Including a .txt into a AppleScriptobjc Xcode project

Marius_Magnificus wrote:



I have not bundle the application yet since I'm still developing it.


If you have an AppleScriptobjc Xcode project you do have a bundle.

Marius_Magnificus wrote:

I tried different things following your steps but with no success.
When I drag the .txt into the project folder I'm prompt to select a

'Destination': Copy items if needed (I check it)
'Added folders': (I check Create folder references)
'Add to targets': (I check my project)


Don't check Create folder references. Delete the file and the enclosing folder (in Xcode) and add the file again.

Marius_Magnificus wrote:


And here it is the code I've used in the AppDelegate file to test it:

Applescript:

set theFile to current application's NSBundle's mainBundle()'s pathForResource:"MyFile" ofType:"txt"


Of course "MyFile" must match the file name in the bundle. And use the Cocoa API to read the file

Applescript:

property |⌘| : a reference to current application
set filePath to |⌘|'s NSBundle's mainBundle()'s pathForResource:"MyFile" ofType:"txt"
set theText to (|⌘|'s NSString's stringWithContentsOfFile:filePath encoding:(|⌘|'s NSUTF8StringEncoding) |error|:(missing value)) as text
set theParagraphs to paragraphs of theText

Last edited by StefanK (2020-02-26 04:36:26 pm)


regards

Stefan

Offline

 

#5 2020-02-26 08:46:55 pm

Marius_Magnificus
Member
Registered: 2019-05-05
Posts: 16

Re: Including a .txt into a AppleScriptobjc Xcode project

Excellent!

I love this community.

When I started my project, it was only based in vanilla AppleScript and then I realized that at least a basic knowledge of Object C and now COCOA API is necessary to go ahead with the features I want to have my app.

Tomorrow is a day off for me, so I will be digging in into the 'Everyday AppleScriptObjC 3ed' I've just get. smile

Thank you for your help, StefanK

Best,

Marius

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)