@@ -53,6 +53,8 @@ export const getGeneratedCode = createSelector([
5353 getReplayEnabled , getDescriptorOptions , getInspectorSettings , getIndentString ,
5454] , ( selected , autoActive , treePath , replayEnabled , descOptions , settings , tab ) => {
5555
56+ let shouldShowTokenify = false ;
57+
5658 function makeNicePropertyPath ( segments : string [ ] ) : string {
5759 const regex = / ^ [ a - z A - Z _ $ ] [ 0 - 9 a - z A - Z _ $ ] * $ / m;
5860
@@ -93,27 +95,27 @@ export const getGeneratedCode = createSelector([
9395
9496 const res : BatchPlayCommandOptionsExtended = { } ;
9597
96- if ( hasAnySync ) { res . synchronousExecution = true ; }
97- else if ( hasAnyAsync ) { res . synchronousExecution = false ; }
98+ if ( hasAnySync ) { res . synchronousExecution = true ; }
99+ else if ( hasAnyAsync ) { res . synchronousExecution = false ; }
98100
99- if ( modalIsExecute ) { res . modalBehavior = "execute" ; }
100- else if ( modalIsWait ) { res . modalBehavior = "wait" ; }
101- else if ( ! modalIsDefault ) { res . modalBehavior = "fail" ; }
101+ if ( modalIsExecute ) { res . modalBehavior = "execute" ; }
102+ else if ( modalIsWait ) { res . modalBehavior = "wait" ; }
103+ else if ( ! modalIsDefault ) { res . modalBehavior = "fail" ; }
102104
103105 return res ;
104106 }
105107
106108 // adds indentation
107- function idt ( str :string ) :string {
109+ function idt ( str : string ) : string {
108110 return str . split ( "\n" ) . map ( l => tab + l ) . join ( "\n" ) ;
109111 }
110112
111- function udt ( str : string ) : string {
113+ function udt ( str : string ) : string {
112114 return str . split ( "\n" ) . map ( l => l . replace ( tab , "" ) ) . join ( "\n" ) ;
113115 }
114116
115117 // replaces quotes
116- function qts ( str :string ) :string {
118+ function qts ( str : string ) : string {
117119 if ( settings . singleQuotes ) {
118120 str = str . replaceAll ( `"` , `'` ) ;
119121 }
@@ -124,13 +126,23 @@ export const getGeneratedCode = createSelector([
124126 const wrappers = settings . codeWrappers ;
125127
126128 if ( selected . length >= 1 || autoActive ) {
127- let data :any = null , iDesc : IDescriptor [ ] = [ ] ;
129+ let data : any = null , iDesc : IDescriptor [ ] = [ ] ;
128130
129131 const stringifyOptions = {
130132 singleQuotes : settings . singleQuotes ,
131133 indent : tab ,
134+ transform : ( input : any | object , prop : number | string | symbol , originalResult : string ) => {
135+ if ( settings . tokenify && prop === "_path" ) {
136+ shouldShowTokenify = true ;
137+ console . log ( input , originalResult ) ;
138+ const res = qts ( `await tokenify("${ input . _path . replaceAll ( "\\" , "/" ) } ")` ) ;
139+ return res ;
140+ }
141+ return originalResult ;
142+ } ,
132143 } ;
133144
145+ const tokenifyInfo = "// Please make sure that file system access permission in manifest.json has correct value.\n\n" ;
134146 const playAbleInfo = "// Alchemist can't generate code from the reply of replay and from dispatched code." ;
135147 const notifierWarning = "// Events recognized as notifiers are not re-playable in most of the cases. There is high chance that generated code won't work.\n\n" ;
136148
@@ -139,7 +151,7 @@ export const getGeneratedCode = createSelector([
139151 } else if ( autoActive ) {
140152 iDesc = [ autoActive ] ;
141153 }
142- if ( iDesc . some ( item => [ "replies" , "dispatcher" ] . includes ( item . originalReference . type ) ) ) {
154+ if ( iDesc . some ( item => [ "replies" , "dispatcher" ] . includes ( item . originalReference . type ) ) ) {
143155 return playAbleInfo ;
144156 }
145157 data = iDesc . map ( item => addPerItemOptions ( item ) ) ;
@@ -149,20 +161,20 @@ export const getGeneratedCode = createSelector([
149161 if ( Array . isArray ( data ) ) {
150162 data . forEach ( d => {
151163 if ( settings . hideDontRecord ) {
152- delete d . dontRecord ;
164+ delete d . dontRecord ;
153165 }
154166 if ( settings . hideForceNotify ) {
155- delete d . forceNotify ;
167+ delete d . forceNotify ;
156168 }
157169 if ( settings . hide_isCommand ) {
158170 delete d . _isCommand ;
159- }
171+ }
160172 } ) ;
161173 }
162174
163175 for ( let i = 0 ; i < data . length ; i ++ ) {
164176 const item = data [ i ] ;
165- RawDataConverter . convertFakeRawInCode ( item , descOptions ) ;
177+ RawDataConverter . convertFakeRawInCode ( item , descOptions ) ;
166178 }
167179
168180 let strPinned = "" ;
@@ -175,34 +187,46 @@ export const getGeneratedCode = createSelector([
175187 const commandOptions = addCommonOptions ( iDesc ) ;
176188
177189 const strOptions = idt ( stringifyObject ( commandOptions , stringifyOptions ) ) ;
178- const strDesc :string = stringifyObject ( data , stringifyOptions ) ;
190+ const strDesc : string = stringifyObject ( data , stringifyOptions ) ;
179191
180192
181193 const strExecModalImport = addModules ? qts ( `const {executeAsModal} = require("photoshop").core;\n` ) : "" ;
182- const strBatchPlayImport = addModules ? qts ( `const {batchPlay} = require("photoshop").action;\n\n` ) : "" ;
194+ const strBatchPlayImport = addModules ? qts ( `const {batchPlay} = require("photoshop").action;\n` ) : "" ;
195+ const strTokenifyImport = addModules && shouldShowTokenify ? qts ( `const {localFileSystem: fs} = require("uxp").storage;\n` ) : "" ;
196+ const strNewLine = addModules ? "\n" : "" ;
183197
184198 const strBatchPlay = `const result = await batchPlay(\n${ idt ( strDesc ) } ,\n${ strOptions } \n);${ strPinned } ` ;
185199 const strActionCommand = `async function actionCommands() {\n${ idt ( strBatchPlay ) } \n}\n\n` ;
186200
187201 const strCall = qts ( `async function runModalFunction() {\n${ tab } await executeAsModal(actionCommands, {"commandName": "Action Commands"});\n}\n\nawait runModalFunction();\n` ) ;
202+ const strTokenify = shouldShowTokenify ? qts ( `async function tokenify(url){\n${ tab } return fs.createSessionToken(await fs.getEntryWithUrl("file:" + url));\n}\n\n` ) : "" ;
188203
189204 let banner = "" ;
190205 if ( iDesc . some ( item => item . originalReference . type === "notifier" ) ) {
191206 banner = notifierWarning ;
192207 }
208+ if ( shouldShowTokenify ) {
209+ banner += tokenifyInfo ;
210+ }
193211
194212
195- if ( wrappers === "batchPlay" ) {
213+ if ( wrappers === "batchPlay" ) {
196214 return (
197215 banner +
198216 strBatchPlayImport +
217+ strTokenifyImport +
218+ strNewLine +
219+ strTokenify +
199220 strBatchPlay
200221 ) ;
201- } else if ( wrappers === "modal" ) {
222+ } else if ( wrappers === "modal" ) {
202223 return (
203224 banner +
204225 strExecModalImport +
205226 strBatchPlayImport +
227+ strTokenifyImport +
228+ strNewLine +
229+ strTokenify +
206230 strActionCommand +
207231 strCall
208232 ) ;
0 commit comments