Old 12-23-2018, 06:53 AM   #1
Shreebz
Human being with feelings
 
Join Date: Apr 2018
Posts: 33
Default Reascript works on Windows, but not OSX

I've got a lua script that inserts media from a table. I'm reading over text file for file names to grab from a folder, and looping over the table with a repeat loop to put the file names in the table. That's what the f:read is for.

On PC the script works great, but on OSX (10.14.2) it only partially works. On OSX it'll only insert the last filename in the table. So, it seems that the loop isn't iterating fully or correctly.

Has anyone run into this before? Is there a way for me to loop through a table that's more reliable for OSX? The table is being created in the same loop that the media insertion is occurring...maybe I should insert the media in a separate loop after the table is fully iterated?

Thoughts welcome.

Code:
repeat                         -- Loop 1. This loop gets all file names, and closes the file.
      s = f:read ("*l")            -- S is a filename. Read one line. *l returns contents of line
      if s then                    -- If s is true (not nill)
        file_names_table[x] = s    -- New entry. Key = "x" and Value = "s"
        x = x + 1                  -- Increment array index
        reaper.InsertMedia(formattedPath..s, 0)
      end
until not s

Last edited by Shreebz; 12-23-2018 at 07:01 AM.
Shreebz is offline   Reply With Quote
Old 12-24-2018, 08:56 AM   #2
preferred.nomenclature
Human being with feelings
 
Join Date: Dec 2014
Posts: 371
Default

Have you tried debugging by adding logs, counters or some such to get a better understanding what’s actually happening?
preferred.nomenclature is offline   Reply With Quote
Old 12-24-2018, 10:43 AM   #3
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,621
Default

Why not using https://www.lua.org/manual/5.3/manua...pdf-file:lines ?

This is much shorter to code and does the same
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...
Meo-Ada Mespotine is offline   Reply With Quote
Old 12-24-2018, 01:43 PM   #4
Shreebz
Human being with feelings
 
Join Date: Apr 2018
Posts: 33
Default

I use logs, and the loop cycles how I'd expect according to them. I think it's a bug with the InsertMedia() function being in a loop. I actually broke this out into two loops. One loop to create the table needed, and a second loop that only inserts the media. Same result.

Can anyone confirm that the InsertMedia() function doesn't iterate correctly when called in a loop?
Shreebz is offline   Reply With Quote
Old 12-24-2018, 01:46 PM   #5
Shreebz
Human being with feelings
 
Join Date: Apr 2018
Posts: 33
Default

Quote:
Originally Posted by mespotine View Post
Why not using https://www.lua.org/manual/5.3/manua...pdf-file:lines ?

This is much shorter to code and does the same
That's a good tip, thank you for it, but doesn't address the core issue I'm having. If I rewrite this, then I will do it with that scheme.
Shreebz is offline   Reply With Quote
Old 12-24-2018, 02:22 PM   #6
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,621
Default

If you're willing to experiment, you can use my Ultraschall-API, that has a dedicated insert mediaitem from file-function with more parameters:

https://mespotin.uber.space/Mespotin...iaItemFromFile

it works differently than the InsertMediaItem-function you use.


On how to install the Ultraschall-API, download it from:
https://mespotin.uber.space/Mespotin...Downloads.html

and for usage/installation, see this page:
https://mespotin.uber.space/Mespotin...002_How_to_Use
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...
Meo-Ada Mespotine is offline   Reply With Quote
Old 12-24-2018, 04:44 PM   #7
preferred.nomenclature
Human being with feelings
 
Join Date: Dec 2014
Posts: 371
Default

I wonder if it has something to do with Windows vs. Mac newlines when reading the file (\r\n vs. \n) ?
preferred.nomenclature is offline   Reply With Quote
Old 12-25-2018, 09:12 AM   #8
Shreebz
Human being with feelings
 
Join Date: Apr 2018
Posts: 33
Default

Quote:
Originally Posted by preferred.nomenclature View Post
I wonder if it has something to do with Windows vs. Mac newlines when reading the file (\r\n vs. \n) ?
I think you're right. The files are added to the project bay, but when I right-click an unavailable file and select "rename" there is an extra return carriage in the name. I bet this is causing the issue, and also explains why only the last item in the list is properly imported as the last item has no return carriage below it.

That being said, do you have an idea on how I could make sure this works properly on OSX? Like I said earlier, it works well on Windows.
Shreebz is offline   Reply With Quote
Old 12-25-2018, 09:55 AM   #9
preferred.nomenclature
Human being with feelings
 
Join Date: Dec 2014
Posts: 371
Default

I’m just getting into Lua myself but surely there’s a way to find and replace “\r” with “” in yor parsed filename before adding it to your table?
preferred.nomenclature is offline   Reply With Quote
Old 12-25-2018, 01:32 PM   #10
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,621
Default

If this is the case, you need to check for
\r\n

on windows and for
\n
on Mac.

Do you use the same file with filenames on Mac and Windows? Or do you create them individually on each system?
The latter case should(tm) be taken care of by the for line-loop, I posted to you earlier.
If its the former, I haven't an idea.

You should output into the ReaScript-Console all filenames returned from the file. If it's missing one or more, then its a hint to the new-line-problem.

Could you post also the file with the filenames you used in both scripts, that caused the problem on Mac?
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...
Meo-Ada Mespotine is offline   Reply With Quote
Old 12-25-2018, 01:35 PM   #11
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,621
Default

Quote:
Originally Posted by preferred.nomenclature View Post
I’m just getting into Lua myself but surely there’s a way to find and replace “\r” with “” in yor parsed filename before adding it to your table?
Yes, something like:
Code:
filestring=string.gsub(filestring, "\r", "")
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...
Meo-Ada Mespotine is offline   Reply With Quote
Old 12-26-2018, 09:26 AM   #12
Shreebz
Human being with feelings
 
Join Date: Apr 2018
Posts: 33
Default

Quote:
Originally Posted by mespotine View Post
If this is the case, you need to check for
\r\n

on windows and for
\n
on Mac.

Do you use the same file with filenames on Mac and Windows? Or do you create them individually on each system?
The latter case should(tm) be taken care of by the for line-loop, I posted to you earlier.
If its the former, I haven't an idea.

You should output into the ReaScript-Console all filenames returned from the file. If it's missing one or more, then its a hint to the new-line-problem.

Could you post also the file with the filenames you used in both scripts, that caused the problem on Mac?
So, that line loop should, by default, format the filename list to be the way that's needed here? It won't require further string manipulation for different operating systems?

It sounds like I need to rewrite this with your recommended scheme, and that'll solve it. I'll do that.

Thanks for the tips.

Last edited by Shreebz; 12-26-2018 at 10:27 AM.
Shreebz is offline   Reply With Quote
Old 12-27-2018, 12:26 PM   #13
Shreebz
Human being with feelings
 
Join Date: Apr 2018
Posts: 33
Default

Thanks for the help here. I finally got it working on both operating systems.

The file:lines() function worked well, but didn't remove the return carriage by default. So, I rewrote the script for naught, as I had to format the return carriage in each line's string anyway. No big deal, though. I'm pretty green, so this was good refactoring experience, and it's good to know what's more efficient. Thanks for the tips.

My stretch goal with this is to create some kind of error logging. Write a text file to the project directory that shows the files that weren't copied...not sure on how to do this just yet.
Shreebz is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 03:19 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.