Skip to main content
Inspiring
January 24, 2019
Answered

Is there a way to set a .txt file to different variables in AppleScript?

  • January 24, 2019
  • 2 replies
  • 489 views

What I'm looking to do is load a text file on my desktop into an AppleScript and each line would be a new variable.

Like this:

45678.jpg

spaceship.tif

bookName.tif

etc.

Then those variables would be

set varName1 to 45678l.jpg

set varName2 to spaceship.tif

set varName3 to bookName.tif

This topic has been closed for replies.
Correct answer Michel Lemieux

Hi David,

I am a bit confused here, why couldn't you simply use "item x of whatever" instead of named variables? Since those "names" do not convey what they are holding...

In any case, you could create those variables in your script like this:

set valueFile to ((path to desktop folder) as text) & "values.txt"

set myValues to read file valueFile using delimiter {return}

try

  set varName1 to item 1 of myValues

end try

try

  set varName2 to item 2 of myValues

end try

try

  set varName3 to item 3 of myValues

end try

try

  set varName4 to item 4 of myValues

end try

try

  set varName5 to item 5 of myValues

end try

...

You just need to put enough "try blocks" to deal with whatever text file you wish to pass to the script. (btw those try block can easily be created with a script if there's too many of them to create.)

But, I would rather use the more simple way of just addressing the "values" by their item number :

set valueFile to ((path to desktop folder) as text) & "values.txt"

-- Contents of values.txt:

-- 2019/01/28

-- Text Value1

-- 100

-- true

-- 2,5

set myValues to read file valueFile using delimiter {return}

set myVars to {}

repeat with i from 1 to count of myValues

  set end of myVars to my detectVarType(item i of myValues)

end repeat

set resultText to ""

repeat with i from 1 to count of myVars

  set myVar to item i of myVars

  set resultText to resultText & i & ": [" & class of myVar & "] -> " & myVar & return

end repeat

display dialog resultText

on detectVarType(aValue)

  if aValue is "true" then

  set aValue to true

  else if aValue is "false" then

  set aValue to false

  end if

  try

  set aValue to aValue as real

  if aValue mod 10 = 0 then

  set aValue to aValue as integer

  end if

  end try

  try

  set aValue to date aValue

  end try

  return aValue

end detectVarType

As a bonus, I added a handler that determine the type of variable you are attempting to read.

HTH

Michel

2 replies

Michel Lemieux
Michel LemieuxCorrect answer
Inspiring
January 24, 2019

Hi David,

I am a bit confused here, why couldn't you simply use "item x of whatever" instead of named variables? Since those "names" do not convey what they are holding...

In any case, you could create those variables in your script like this:

set valueFile to ((path to desktop folder) as text) & "values.txt"

set myValues to read file valueFile using delimiter {return}

try

  set varName1 to item 1 of myValues

end try

try

  set varName2 to item 2 of myValues

end try

try

  set varName3 to item 3 of myValues

end try

try

  set varName4 to item 4 of myValues

end try

try

  set varName5 to item 5 of myValues

end try

...

You just need to put enough "try blocks" to deal with whatever text file you wish to pass to the script. (btw those try block can easily be created with a script if there's too many of them to create.)

But, I would rather use the more simple way of just addressing the "values" by their item number :

set valueFile to ((path to desktop folder) as text) & "values.txt"

-- Contents of values.txt:

-- 2019/01/28

-- Text Value1

-- 100

-- true

-- 2,5

set myValues to read file valueFile using delimiter {return}

set myVars to {}

repeat with i from 1 to count of myValues

  set end of myVars to my detectVarType(item i of myValues)

end repeat

set resultText to ""

repeat with i from 1 to count of myVars

  set myVar to item i of myVars

  set resultText to resultText & i & ": [" & class of myVar & "] -> " & myVar & return

end repeat

display dialog resultText

on detectVarType(aValue)

  if aValue is "true" then

  set aValue to true

  else if aValue is "false" then

  set aValue to false

  end if

  try

  set aValue to aValue as real

  if aValue mod 10 = 0 then

  set aValue to aValue as integer

  end if

  end try

  try

  set aValue to date aValue

  end try

  return aValue

end detectVarType

As a bonus, I added a handler that determine the type of variable you are attempting to read.

HTH

Michel

Community Expert
January 24, 2019

Hi David

I am no expert in Applescript just dabble in it sometimes, the following code could work

set newFile to ("Macintosh HD:Users:Manan:Desktop:Test.txt")//Change the path to the path on your MAC

set theFileContents to paragraphs of (read file newFile)

set variable1 to item 1 of theFileContents as text

set variable2 to item 2 of theFileContents as text

set variable3 to item 3 of theFileContents as text

As far as i could search there is no way to create dynamic variable names in Applescript, i.e. variable names are generated at compile time(i may be wrong, someone more experienced could verify it). So there is no way to tell how many variables to create. I am not sure why you want to create separate variables, you have a list of lines in theFileContents, over which you can loop and do your work.

-Manan

-Manan